2011-09-30 50 views
0

我有視圖中的2的UITableView和我加入UITableViewCellAccessoryDisclosureIndicator僅在第二錶行5和行7,iOS UITableView Cell滾動後加載不正確?

但滾動第二表下來之後(其第1行消失),然後滾動返回頂部(第1行出現),第1行現在有UITableViewCellAccessoryDisclosureIndicator ?!第1行莫名其妙地變成第5行還是第7行?以下是我的代碼cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.textColor = [UIColor blueColor]; 
    cell.detailTextLabel.textColor = [UIColor blackColor]; 
    if (tableView == table1) 
    { 
     cell.textLabel.text = [title1 objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text = [list1 objectAtIndex:indexPath.row]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    else if (tableView == table2) 
    { 
     cell.textLabel.text = [title2 objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text = [list2 objectAtIndex:indexPath.row]; 
     if (indexPath.row == 5 || indexPath.row == 7) 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
     else 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
    } 
    return cell; 
} 

非常感謝!

+0

所以,你有2個tableViews 1個視圖,右高於/低於對方?如果是這樣,爲什麼? – WrightsCS

+0

因爲我想要2個表具有不同的寬度,否則我會使用2個部分的1個表。你推薦一些其他的方式嗎?謝謝! – iceholder

回答

2

UITableViewCells被重用來優化性能。這發生在[tableView dequeueReusableCellWithIdentifier:CellIdentifier];您需要在每次調用tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 時明確設置單元格上的任何屬性。

像這樣的事情應該可以解決這個問題:

if (indexPath.row == 5 || indexPath.row == 7) 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
     else 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
+0

問題修復!!!感謝你的回答 :) – iceholder

相關問題