2011-02-28 38 views
0

我有一個奇怪的行爲加載表。Iphone SDK - 奇怪的行爲與UITableView和UITableViewCellAccessory

該表有時會加載正常,有時加載UITableViewCellAccessory被添加到最後一節的某些行。

這裏是的cellForRowAtIndexPath代碼

- (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 [email protected]"Monday"; 

     if([enabled isEqualToString:[section4 objectAtIndex:0]]){ 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
     return cell; 



    } 
    if(indexPath.row == 1) 
    { 
     cell.textLabel.text [email protected]"Tuesday"; 

     if([enabled isEqualToString:[section4 objectAtIndex:1]]){ 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
     return cell; 


    } 
    if(indexPath.row == 2) 
    { 
     cell.textLabel.text [email protected]"Wednesday"; 

     if([enabled isEqualToString:[section4 objectAtIndex:2]]){ 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
     return cell; 


    } 
    if(indexPath.row == 3) 
    { 
     cell.textLabel.text [email protected]"Thursday"; 

     if([enabled isEqualToString:[section4 objectAtIndex:3]]){ 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
     return cell; 



    } 
    if(indexPath.row == 4) 
    { 
     cell.textLabel.text [email protected]"Friday"; 

     if([enabled isEqualToString:[section4 objectAtIndex:4]]){ 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
     return cell; 



    } 
    if(indexPath.row == 5) 
    { 
     cell.textLabel.text [email protected]"Saturday"; 

     if([enabled isEqualToString:[section4 objectAtIndex:5]]){ 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
     return cell; 



    } 
    if(indexPath.row == 6) 
    { 
     cell.textLabel.text [email protected]"Sunday"; 

     if([enabled isEqualToString:[section4 objectAtIndex:6]]){ 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
     return cell; 



    } 


} 
return cell; 

}

最後一行有UITableViewCellAccessoryDe​​tailDisclosureButton,但我從來沒有設置這個按鈕本條及隨機出現:

感謝

回答

1

你正在獲得之前用於第2部分的再循環細胞,因此具有泄露按鈕。由於您只在啓用該日期的情況下更改accessoryType,因此如果未啓用披露按鈕,則永不刪除披露按鈕。解決這個問題

最簡單的方法就是做:

cell.accessoryType = UITableViewCellAccessoryNone; 

你出列/創建單元格之後。