【问题标题】:UITableView not refreshing appropriatelyUITableView 没有适当刷新
【发布时间】:2010-08-11 02:48:38
【问题描述】:

我有一个 selectedArray 定义为我的文件中的一个属性,它保存了哪些单元格已被“检查”。在显示单元格时,我检查当前单元格值是否在 selectedArray 中,如果是,我将 UITableViewCellAccessoryCheckmark 显示为表格的附件视图。

唯一的问题是,当用户使用 UISearchBar 通过 UITableView 进行搜索时,我要求表在完成搜索后重新加载数据,并且在 numberofRows 方法中,我看到 selectedArray 中的对象数量正确。但是,cellForRowatIndexPath 方法仍然显示旧的复选标记,就好像 selectedArray 从未更新过一样。

任何人都可以帮助我吗?这是我的代码 -

NSArray *tempArray = [[NSArray alloc] init];
if(tableView == theTable) {
    tempArray = [contactsArray objectAtIndex:indexPath.row];
} else {
    tempArray = [searchfor_array objectAtIndex:indexPath.row];
}

NSString *cellValue = [tempArray objectAtIndex:0];
static NSString *CellIdentifier;
CellIdentifier = cellValue;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 200, 20)];
    name.text = [tempArray objectAtIndex:0];
    name.backgroundColor = [UIColor clearColor];
    [name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
    [cell.contentView addSubview:name];
    [name release];

    if([selectedArray containsObject:[tempArray objectAtIndex:0]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

}

return cell;
[cell release];
[tempArray release];
[cellValue release];

【问题讨论】:

    标签: objective-c iphone uitableview ios4


    【解决方案1】:

    看起来好像您只设置了一次单元并重复使用它。单步执行代码,您应该会看到 if 块仅在第一次加载单元格时输入。

    您需要调整每一行的单元格,如下所示:

    if (cell == nil) {
        // Initialize cell.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Adjust cell for each row.
    if([selectedArray containsObject:[tempArray objectAtIndex:0]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    

    【讨论】:

    • 当我这样做时,它并没有解决问题并且标签开始相互堆叠......
    • 我没有详细说明如何设置单元格并且错过了清除附件视图的其他内容。拉斐尔已经搞定了。
    【解决方案2】:

    添加到贾斯汀的答案 -

    您应该只在 if(cell == nil) 部分添加标签。您在该语句之外处理附件视图。它应该是这样的 -

    if (cell == nil) {
    
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
        UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 200, 20)];
        name.text = [tempArray objectAtIndex:0];
        name.backgroundColor = [UIColor clearColor];
        [name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        [cell.contentView addSubview:name];
        [name release];
    
    
    }
    
    if([selectedArray containsObject:[tempArray objectAtIndex:0]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    

    【讨论】:

    • 感谢您填补我的回答中的空白。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多