Iphone:UITableview中的选中标记在滚动时会混淆 [英] Iphone: Checkmarks in UITableview get mixed up when scrolling

查看:84
本文介绍了Iphone:UITableview中的选中标记在滚动时会混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我在UITableView中应用于我的行的复选标记在滚动时全部混淆了。我很确定这与iphone如何重复使用单元格有关,当我向上滚动时有一个复选标记,它可能会在我有机会时将其重新放入。

I have a bit of a problem where my check marks that i apply to my rows in my UITableView get all mixed up when i scroll. Im pretty sure this has to do with how the iphone reuses the cells and when i scroll away from on that has a check mark it probably puts it back in when i gets a chance.

有人可以给我一些提示,告诉我如何避免这种情况,或者可能看看我的方法,看看有什么东西可以看看吗?

Could someone please give me some tips on how I might avoid this or possibly take a look at my methods and see if anything looks off?

我在想也许我可以保存用户所做的每一行选择,然后检查哪些行正在显示,以确保正确的行得到了复选标记,但我无法看到这样做。

I was thinking that maybe I could save each row selection that the user made and then check to see which rows were being displayed to make sure the correct ones got the checkmark but I could'nt see a way to do so.

非常感谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    [cell setAccessoryView:nil];
}

NSMutableArray *temp = [[NSMutableArray alloc]init];
for (int j = 0; j < [listOfRowersAtPractice count]; j++) {
    if ([[differentTeams objectAtIndex:indexPath.section] isEqualToString:[[rowersAndInfo objectForKey:[listOfRowersAtPractice objectAtIndex:j]]objectForKey:@"Team"]]) {
        [temp addObject:[listOfRowersAtPractice objectAtIndex:j]];
    }
}

[cell.cellText setText:[temp objectAtIndex:indexPath.row]]; 

[temp removeAllObjects];
[temp release];
// Set up the cell...


return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell.accessoryType != UITableViewCellAccessoryCheckmark) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }


推荐答案

是在您将单元格重置为默认状态并检查行状态并更改状态后,保存所选行的状态并在cellforrowatindexpath中保存。

Yes save the state of the row which is selected and in cellforrowatindexpath after you get the cell reset it to default state and check the state of the row and change the state.

编辑:

您可以创建一个NSMutabaleArray,其项目数等于数据源中的项目数,即代码中的名称temp。

You can create a NSMutabaleArray with number of items equal to the number of items in your datasource which is the name temp in your code.

在选择中,您实际上可以将该索引处的值更改为某些文本,例如上面创建的数组中的@selected。

On select you can actually change the value at that index to some text like @"selected" in the above created array.

在您的cellforrowatindexpath如果选中或未选中,则可以检查此文本,然后更改单元格的属性。它就像为选定和未选择状态维护位图状态一样。

In your cellforrowatindexpath you can check this text if its selected or unselected and then change the property of the cell. Its like maintaining a bitmap state for selected and unselected states.

这篇关于Iphone:UITableview中的选中标记在滚动时会混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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