滚动时,UITableView无法正确更新 [英] UITableView not updating correctly when scrolling

查看:34
本文介绍了滚动时,UITableView无法正确更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个包含2个部分的UITableView.表格第一次加载时,所有单元格都显示正确的信息,但是当我开始上下滚动时,单元格detailTextLabel和annexType的刷新不正确,因此一些只应包含detailedTextLabel的单元格也包含附件,并且应该只包含一个附件,还应该包含一个detailTextLabel.

I am writing a UITableView that contains 2 sections. When the table first loads all the cells are displaying the correct info, but when I start scrolling up and down, cells detailedTextLabel and accessoryType are being refreshed incorrectly, such that some cells that should only contain a detailedTextLabel also contain an accessory, and cells that should only contain an accessory also contain a detailedTextLabel.

内部 cellForRowAtIndexPath::我使用嵌套的switch/case语句将正确的值应用于它们各自的节/行中的单元格.据我所知,这些语句中的逻辑是正确的,那么更新时 cell 变量的值是否可能不正确?

Inside cellForRowAtIndexPath: I am using nested switch/case statements to apply the correct values to the cells in their respective section/row. As far as i can tell the logic in these statements is correct, so is it possible that the value of cell variable is inccorect when updating?

该表已正确加载,但是在滚动了附件类型和详细的文本标签后,将它们混在一起.

The table loads correctly but after scrolling accessoryType and detailedTextLabel gets mixed up.

单击以链接到表格的屏幕截图.

这是我的UITableViewController子类中的代码:

Here is the code inside of my UITableViewController subclass:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [sectionNames count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSArray *headingsSection = [cellTitles objectAtIndex:section];
    return [headingsSection count]; 
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [sectionNames objectAtIndex:section];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    self.tableView.allowsSelection = YES;
    static NSString *CellIdentifier = @"Cell";

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

    cell.textLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 

    switch (indexPath.section) {
    case 0:
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d%%", [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]];
        break;
    case 1:
        switch (indexPath.row) {
        case 0:
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 1:
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 2:
            if (defaultAssistOn) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            } else {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            break;
        }
        break;
    }

    return cell;
}

推荐答案

由于单元格的重新使用,具有先前设置值的单元格会重新出现.

Due to cell re-use, cells with previously set values re-appear.

开关(indexPath.section)... 之前,将detailTextLabel和AccessoriesType初始化为默认值:

Before the switch (indexPath.section) ..., initialize the detailTextLabel and accessoryType to defaults:

cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryNone;

这篇关于滚动时,UITableView无法正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆