【问题标题】:select rows from table in ios从ios中的表中选择行
【发布时间】:2013-06-08 19:18:06
【问题描述】:

我是 xcode 的新手,我正在使用此代码从我的表中最多选择 10 行。此代码正在运行,但它存在一个问题,假设当我从中选择一行时,我的选择会自动选择其他一些值。我不明白这是什么错误,请帮我删除这个错误。谢谢

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
    if(count < 10)
    {
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [selectedobjects addObject:[NSNumber numberWithInt:indexPath.row]];
        count++;
    }

} else {
    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    [selectedobjects removeObject:[NSNumber numberWithInt:indexPath.row]];
    count --;
}
}

【问题讨论】:

    标签: ios uitableview row selection


    【解决方案1】:

    这是由于单元格重用。您需要检查 cellForRowAtIndexPath 中选定的单元格并相应地指定附件类型。最好将所选项目的 indexPath 保存在 didSelectRowAtIndexPAth 中,而不是将 indexPath.row 保存为 selectedObject。然后做

     - (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
      // Do other stuff
      if ([selectedobjects containsObject:indexPath]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
      }
      else
       {
         [cell setAccessoryType:UITableViewCellAccessoryNone];
       }
    }
    

    【讨论】:

    • 我也这样做了,但是当我开始向下滚动 ma 表时,当我回来时,我选择的行自动取消选择.. :(
    • if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // 做其他事情 if ([selectedobjects containsObject:indexPath]) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } 其他 { [单元格 setAccessoryType:UITableViewCellAccessoryNone]; } cell.textLabel.text = [myArray objectAtIndex:indexPath.row]; //=@"ASDF" 有效。返回单元格;
    • 您确定将代码放在 if(cell == nil) 之外并且您的 selectedObjects 包含选定的 indexPaths
    【解决方案2】:

    在你的 .h 中取一个 NSMutable 字典,不要为字典做任何属性和合成。

    在 CellForRowAtIndex 中使用这一行

    if([tickmarkDictionary objectForKey:[NSString stringWithFormat:@"%d",[indexPath row]]])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    

    并使用 DidSelectRowAtIndex 方法,如下所示。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *thisCell= (UITableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
    
        if (thisCell.accessoryType == UITableViewCellAccessoryNone)
        {
            thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
    
            [tickmarkDictionary setValue:[NSString stringWithFormat:@"%@",thisCell.textLabel.text] forKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
        }
        else
        {
            if([tickmarkDictionary objectForKey:[NSString stringWithFormat:@"%d$%d",[indexPath section],[indexPath row]]])
            {
              [tickmarkDictionary removeObjectForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
    
            }
            NSLog(@"dict count %d",[tickmarkDictionary count]);
            thisCell.accessoryType = UITableViewCellAccessoryNone;
        }
    
         NSLog(@"dict count %d",[tickmarkDictionary count]);
    }
    

    现在,当您开始向上和向下滚动表格时,当您回到自动选择的行时,不会取消选择。

    试试这个。希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2018-08-27
      • 2012-02-27
      • 2016-10-29
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多