iphone - UITableView 滚动问题

标签 iphone uitableview

我在我的应用程序中创建了一个 TableView,其中包含 5 个部分。

第 1 - 4 部分仅包含一分钟的一行,第 5 部分包含 5 行。

一切正常,直到我将 TableView 滚出屏幕。在我的第一个部分和行(单元格)中,我将accessoryView设置为UILabel,其中包含一些文本。

每个其他单元格都将公开按钮作为附件类型。

当我滚动 tableView 时,第一个单元格中的文本不知何故出现在最后一个单元格中!?

我通过将 NSString 添加到数组,然后将它们作为字典添加到 NSMutableArray 来设置数据。

这是我的单元设置:

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

   // UISwitch *aUISwitch = [[[UISwitch alloc]initWithFrame:CGRectZero]autorelease];

    // Configure the cell. 

    NSDictionary *dictionary = [listOfSettings objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Settings"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    if([cellValue isEqualToString:@"Status"]){
        UILabel *viewLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
        [viewLabel setText:@"Connected"];
        cell.accessoryView = viewLabel;
        [viewLabel release];

    }
    else{
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }
    return cell;
}

我知道单元格在离开屏幕时会被删除/移除,所以我认为这与此有关?处理离开屏幕并重新出现的单元格的推荐做法是什么?

最佳答案

乍一看...在else语句中,不仅需要设置cell.accessoryType,还需要设置cell.accessoryView=nil;

当单元格被回收时,accesoryView 仍然存在。

关于iphone - UITableView 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5833889/

相关文章:

ios - 更改 UISearchController 在 Swift 中的位置

iphone - iphone 模拟器和设备之间的屏幕标签颜色不同?

ios - 在点击 iOS 时重新激活分段控制

iphone - iPhone中能否获取 "Default Alarm"信息?

ios - 如何调试 UITableView 不显示行?

ios - 具有异步图像加载的自调整单元格大小

iphone - iPhone 版 Speex

iphone - 保存时出现核心数据 Unresolved 错误

ios - UITableviewcells 侧面不需要的动画

ios - 如何克服 UITableViewCell 子类限制