2

我对 iphone 开发相当陌生,我正在尝试使用 UITableViewCell 创建我自己的“邀请你的朋友”系统。我检查了其他帖子,但我不知道如何解决它。我现在遇到的一个问题是,我的 UITableViewCell 附件正在重复。例如。当我单击此复选框时。

http://min.us/mCHcd3kwI 这是我的项目链接,如果有人决定看的话

在此处输入图像描述

如果我向下滚动,其他复选框也会被标记

在此处输入图像描述

我想知道我该如何解决这个问题。

这是我的代码

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
switch (section) {
    case 0:
        return 4;
        break;
    case 1:
        return [aArray count];
        break;
    case 2:
        return [bArray count];
        break;
    case 3:
        return [cArray count];
        break;
    case 4:
        return [dArray count];
        break;
    case 5:
        return [eArray count];
        break;
    case 6:
        return [fArray count];
        break;
    case 7:
        return [gArray count];
        break;
    case 8:
        return [hArray count];
        break;
    case 9:
        return [iArray count];
        break;
    case 10:
        return [jArray count];
        break;
    case 11:
        return [kArray count];
        break;
    case 12:
        return [lArray count];
        break;
    case 13:
        return [mArray count];
        break;
    case 14:
        return [nArray count];
        break;
    case 15:
        return [oArray count];
        break;
    case 16:
        return [pArray count];
        break;
    case 17:
        return [qArray count];
        break;
    case 18:
        return [rArray count];
        break;
    case 19:
        return [sArray count];
        break;
    case 20:
        return [tArray count];
        break;
    case 21:
        return [uArray count];
        break;
    case 22:
        return [vArray count];
        break;
    case 23:
        return [wArray count];
        break;
    case 24:
        return [xArray count];
        break;
    case 25:
        return [yArray count];
        break;
    case 26:
        return [zArray count];
        break;
}}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;

if([tableView isEqual:self.myTableView]){
    static NSString *TableViewIdentifier = @"MyCells";
    cell = [tableView dequeueReusableCellWithIdentifier:TableViewIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewIdentifier];
    }
    NSString *group;
    NSUInteger row = [indexPath row];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;


    if([selectedRowsArray containsObject:[content objectAtIndex:row]]){
        cell.imageView.image = [UIImage imageNamed:@"selected@2x.png"];
    }else {
        cell.imageView.image = [UIImage imageNamed:@"unselected@2x.png"];
    }
    cell.imageView.userInteractionEnabled = YES;


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleChecking:)];
     [cell.imageView addGestureRecognizer:tap];


    switch (indexPath.section) {
        case 0:
            group = [suggestedPeople objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 1:
            group = [aArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 2:
            group = [bArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 3:
            group = [cArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 4:
            group = [dArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 5:
            group = [eArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 6:
            group = [fArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 7:
            group = [gArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 8:
            group = [hArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 9:
            group = [iArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 10:
            group = [jArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 11:
            group = [kArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 12:
            group = [lArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 13:
            group = [mArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 14:
            group = [nArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 15:
            group = [oArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 16:
            group = [pArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 17:
            group = [qArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 18:
            group = [rArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 19:
            group = [sArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 20:
            group = [tArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 21:
            group = [uArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 22:
            group = [vArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 23:
            group = [wArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 24:
            group = [xArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
        case 25:
            group = [yArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;

        case 26:
            group = [zArray objectAtIndex:row];
            cell.textLabel.text = group;
            break;
    }

}
return cell;

}

4

3 回答 3

3

这是因为您仅存储row以确定是否检查了单元格。由于每个部分在索引 0/1 处都有一个单元格,因此这不能如您所愿。

if([selectedRowsArray containsObject:[content objectAtIndex:row]]){
    cell.imageView.image = [UIImage imageNamed:@"selected@2x.png"];
}else {
    cell.imageView.image = [UIImage imageNamed:@"unselected@2x.png"];
}

用这个替换它:(假设你有一个NSMutableSetas a property self.selectedIndexPaths

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    if ([self.selectedIndexPaths containsObject:indexPath])
    {
        cell.imageView.image = [UIImage imageNamed:@"selected@2x.png"];
    }
    else
    {
        cell.imageView.image = [UIImage imageNamed:@"unselected@2x.png"];
    }
    ...
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.selectedIndexPaths containsObject:indexPath])
    {
        [self.selectedIndexPaths addObject:indexPath];
    }
    else
    {
        [self.selectedIndexPaths removeObject:indexPath];
    }

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
于 2012-04-21T04:50:23.910 回答
0

当您每次设置单元格时需要删除子视图/将其设置为零。这是“早期”单元格中的复选标记,因为它正在被表格视图重用。

于 2012-04-21T04:40:12.007 回答
0

如果您使用分组样式,请确保正确处理索引,因为每个部分都将具有相同类型的索引意味着如果您有 2 个部分和 3 行,那么在这两个部分中 index.row 将为每个部分提供 0,1,2部分

而且您还必须处理单元格的可重用属性,即它仅创建当前可见的那些单元格,然后在您滚动表格时再次重用它们,因此当您回滚到您之前检查过的上一个单元格时,将显示给您尽管已检查,但未检查。

希望这会帮助你为我糟糕的英语感到抱歉,因为它不是我的母语:)

于 2012-04-21T06:52:42.857 回答