简体   繁体   English

带有AccessoryView的单个UITableViewCell

[英]Single UITableViewCell with AccessoryView

I'm trying to build an UITableView with some long texts inside each cell. 我正在尝试使用每个单元格内的一些长文本来构建UITableView。 Cells are without AccessoryView, except for one cell (the 8th one), that is a sort of button to open a detail view. 单元格没有AccessoryView,只有一个单元格(第8个)除外,这是一种用于打开详细视图的按钮。

Consider this code: 考虑以下代码:

- (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;
}

It works, but the problem is that when I scroll to the bottom of the Table (the 8th is also the last row) and then I go back to the upper side, another AccessoryView is added to a random point (more or less the 3rd cell, but I don't know if it is inside of it or is floating around randomly). 它有效,但是问题是当我滚动到表格的底部(第8行也是最后一行),然后又回到上侧时,另一个AccessoryView被添加到一个随机点(或多或少是第3行)单元格,但我不知道它是否在其中或随机浮动)。 Is it something related to cell reusing by iOS? 它与iOS单元重用有关吗? How can I avoid it? 我该如何避免呢?

Thanks in advance. 提前致谢。

The cell is being reused (as demonstrated by your call to -dequeueReusableCellWithIdentifer ). 该单元正在被重用(如您对-dequeueReusableCellWithIdentifer的调用-dequeueReusableCellWithIdentifer )。

The answer is to set the cell to wanted defaults after it's been dequeued, or add an else clause to the if statement to handle it. 答案是将单元出队后将其设置为所需的默认值,或在if语句中添加else子句以对其进行处理。

- (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;
}

You have to explicitly set no disclosure button to every cell but the one that you wish to have disclosure. 您不必为每个单元格明确设置任何公开按钮,而是要希望公开的按钮。 This way when the cell gets reused elsewhere its disclosure indicator is removed: 这样,当单元在其他地方重用时,其公开指示器将被删除:

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

This is due to cell reuse as you surmise. 这是由于您推测单元重用。 You must explicitly set UITableViewCellAccessoryNone for cells at index paths other than 7. 您必须为除7以外的索引路径的单元格显式设置UITableViewCellAccessoryNone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM