I’m making an app that needs three buttons in each tableview cell. I tried just adding the buttons to the cell using addSubview in cellForRowAtIndexPath: but this resulted in slow/janky scrolling with more than 6 or 7 rows.
I did some research online and have followed Apple’s example of subclassing the UITableViewCell and drawing everything in drawRect. I can get text and images to draw perfectly, using drawAtPoint but this doesn’t appear to work for UIButtons.
Adding the button as a subview of [self contentView] (in my subclasses drawRect) just results in even worse scroll lag than before.
Does anyone know how to get a button to draw properly within my UITableViewCell subclass?
Getting this right is crucial to the entire app so any help would be greatly appreciated!
UPDATE: Here is the code used for for tableView:cellForRowAtIndexPath
static NSString *CellIdentifier = @"CustomCell";
AHCustomCell * cell = (AHCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
AHCustomCell * customCell = [[[AHCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
customCell.frame = CGRectMake(0.0, 0.0, 320.0, 55.0);
cell = customCell;
}
The following code gives three buttons and no jaggy scrolling. Using tags, you can reset the text of the buttons depending on the row. This is illustrated for button 1 (which adjusts its title according to the row number). The
button1Pressed:method illustrated figures out what row the button press came from. Hope this will be helpful.