Cell accessorytype不会重载[tableview reloadData]

时间:2011-10-10 19:41:04

标签: objective-c ios uitableview checkmark accessorytype

我对iPhone编程很新,所以我的问题可能是由于完全缺乏对基本原理的了解。

我正在开发一款有两个视图的iPhone应用程序。第一个视图有两个按钮,当按下其中一个按钮时,会有一个带有tableview的模态视图弹出窗口。根据按下的按钮,此表视图的填充方式不同。如果按下按钮按钮1,则使用button1Data填充tableview。用户可以选择单元格,如果这样做,则将单元格类型设置为UITableViewCellAccessoryCheckmark。然后我将已检查单元格的名称保存到tableViewListChecked中,以便稍后可以决定在数据更改时应该检查哪个单元格。

问题如下:在取消模态视图后,我选择了button2,仍然在button2Data中选择了button1Data中选中的单元格。我很清楚,函数[theTableView reloadData]正在工作,因为数据确实发生了变化,但是在我从屏幕上滚动单元格之前,accesorytupes仍然是相同的。

P.S。如果我确实滚动了屏幕的单元格,那么就可以正确设置附件类型。

- (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];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }

    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.textLabel.text = [tableViewList objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (![tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [tableViewListChecked addObject:[NSString stringWithString:cell.textLabel.text]];       
    }

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [tableViewListChecked removeObject:[NSString stringWithString:cell.textLabel.text]];
    }   
}

谢谢你的帮助!

3 个答案:

答案 0 :(得分:1)

如果单元格不是nil(如果它已从表视图中出列),那么实际上并不是这样,更改附件类型。放弃其他,即改变

else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
}

if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
}

答案 1 :(得分:0)

听起来每次呈现模态视图时都会重复使用UITableView的相同实例,在这种情况下,tableViewListChecked数组会发生什么?您正在交换tableViewList中的数据;但是你是否采用了一些策略来在按钮1和按钮2之间的按钮之间保留已检查项目列表?

答案 2 :(得分:0)

如果这里的人没有帮助你,你可以设置: 每次用户点击button2时,cell.accessoryType = UITableViewCellAccessoryNone

相关问题