Iphone sdk - Strange behaviour with UITableView and UITableViewCellAccessory
I am having a strange behaviour loading a table.
The table sometimes loads fine and sometimes load UITableViewCellAccessory's are added to some rows of the last section.
Here is the cellForRowAtIndexPath code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSString *enabled= @"1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
cell.textLabel.text =[section1 objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
}
if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
cell.textLabel.text =[section2 objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
}
if(indexPath.section == 2)
{
if(indexPath.row == 0)
{
cell.textLabel.text =[NSString stringWithFormat:@"Morning Time: %@" , [section3 objectAtIndex:0]];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
if(indexPath.row == 1)
{
cell.textLabel.text =[NSString stringWithFormat:@"Afternoon Time: %@" , [section3 objectAtIndex:1]];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
}
if(indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.textLabel.text =@"Monday";
if([enabled isEqualToString:[section4 objectAtIndex:0]]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
if(indexPath.row == 1)
{
cell.t开发者_如何学编程extLabel.text =@"Tuesday";
if([enabled isEqualToString:[section4 objectAtIndex:1]]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
if(indexPath.row == 2)
{
cell.textLabel.text =@"Wednesday";
if([enabled isEqualToString:[section4 objectAtIndex:2]]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
if(indexPath.row == 3)
{
cell.textLabel.text =@"Thursday";
if([enabled isEqualToString:[section4 objectAtIndex:3]]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
if(indexPath.row == 4)
{
cell.textLabel.text =@"Friday";
if([enabled isEqualToString:[section4 objectAtIndex:4]]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
if(indexPath.row == 5)
{
cell.textLabel.text =@"Saturday";
if([enabled isEqualToString:[section4 objectAtIndex:5]]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
if(indexPath.row == 6)
{
cell.textLabel.text =@"Sunday";
if([enabled isEqualToString:[section4 objectAtIndex:6]]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
}
return cell;
}
The last row has a UITableViewCellAccessoryDetailDisclosureButton, but I have never set up this button to this section and occurs randomly:
Thanks
You are getting a recycled cell that was previously used for section 2, and hence has a disclosure button. Since you only change the accessoryType
if the day is enabled, you never remove the disclosure button if it's not enabled.
Easiest way to fix this is to do:
cell.accessoryType = UITableViewCellAccessoryNone;
right after you dequeue/create the cell.
精彩评论