I have a table into wich I’d like to mix some custom cells and standard cells.
I’m not really at ease with custom cells.
To do this, I wrote this code, I guess it’s not optimal :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dataList count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
UITableViewCell *cell;
if (indexPath.row+1 == [tableView numberOfRowsInSection:0]) {
CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
}
else {
CellIdentifier = @"ChooseSounds_cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
UIViewController *c = [[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
cell = (ChooseSoundsOneCell*)c.view;
[c release];
}
[((ChooseSoundsOneCell*)cell).playSoundBtn addTarget:self action:@selector(playSound:event:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
It’s a mix of the default code from defaut TableViewController source, and a source I’ve found here to use a custom cell. There are some little difference beetween both, but as I don’t understand really what is done, I separated the two blocks. I use the standard cell just for the last line.
I could use a custom cell to, but I don’t like writing code or using xibs if there are already existing tools to do what I want to do.
So the question is : what would be the good code to do this ?
The usual pattern is to set the data into the cell in tableView:cellForRowAtIndexPath: not tableView:willDisplayCell.
For the latter the documentation states (in part):
This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color.