2012-09-17 33 views
0

我想在每個單元格內用一些長文本構建一個UITableView。單元格沒有AccessoryView,除了一個單元格(第8個單元格),這是一種打開詳細信息視圖的按鈕。單UITableViewCell與AccessoryView

考慮以下代碼:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGSize size = [[quotes objectAtIndex:indexPath.row] sizeWithFont:16 constrainedToSize:CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)]; 
    return size.height+20; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

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

    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.text = [quotes objectAtIndex:indexPath.row]; 

    if(indexPath.row==7){ 
     [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; 
    } 

    return cell; 
} 

它的工作原理,但問題是,當我滾動到表的底部(8日也是最後一行),然後我回到上方,另一個AccessoryView被添加到一個隨機點(或多或少的第三個單元格,但我不知道它是否在它的內部或隨機浮動)。 是否與iOS重新使用單元有關?我怎樣才能避免它?

在此先感謝。

回答

1

你必須明確地設置沒有披露按鈕,每一個細胞,但你希望有披露的。當細胞被其他地方重新利用其信息披露指標,刪除了此方式:

if(indexPath.row==7){ 
     [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; 
    }else{ 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 
1

該單元格正在重用(如您致電-dequeueReusableCellWithIdentifer所示)。

答案是設置爲默認它已經離隊後的細胞,或添加else條款的if語句來處理它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

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

    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.text = [quotes objectAtIndex:indexPath.row]; 
    // Set to expected default 
    [cell setAccessoryType:UITableViewCellAccessoryNone]; 

    if(indexPath.row==7){ 
     [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; 
    } 

    return cell; 
} 
1

爲您推測這是由於小區重用。您必須明確地設置UITableViewCellAccessoryNone以用於除7以外的索引路徑處的單元。

相關問題