Tatable View多项选择无法正常工作?

时间:2015-09-26 09:17:12

标签: ios objective-c xcode uitableview

我正在尝试在表视图中实现多选。但它没有用。当我添加下面的代码时,它只显示表视图中元素的单个部分。当我从代码中删除此行时,我可以选择但不能取消选择项目。

删除了行代码:多项选择有效,但无法取消选择

for (i=0; i<[arr_selectedCells count]; i++) {
  NSInteger currentValue = [(NSNumber *)[arr_selectedCells objectAtIndex:i] integerValue];
  if (currentValue==indexPath.row) {
    cell.accessoryType=UITableViewCellAccessoryCheckmark;
  }
}

正确。

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    UserFriend *user_friend=[arr_userFriends objectAtIndex:indexPath.row];
    cell.textLabel.text=user_friend.name;
    NSLog(@"name is %@",user_friend.name);
    int i;
    for (i=0; i<[arr_selectedCells count]; i++)
    {
        NSInteger currentValue = [(NSNumber *)[arr_selectedCells objectAtIndex:i]integerValue];

        if(currentValue==indexPath.row)
        {
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType=UITableViewCellAccessoryNone;

        }


    }

    return cell;
}

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

    if(![arr_userFriends containsObject:[NSNumber numberWithInteger:indexPath.row]])
    {
        [arr_selectedCells addObject:[NSNumber numberWithInteger:indexPath.row]];

    }
    else
    {
        [arr_selectedCells removeObject:[NSNumber numberWithInteger:indexPath.row]];

    }

    NSLog(@"numr is %ld",(long)indexPath.row);
    [tableView reloadData];
   }

请告诉我哪里出错了?

2 个答案:

答案 0 :(得分:0)

我刚编辑了你的代码!我希望它对你有用

tableView CellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    UserFriend *user_friend=[arr_userFriends objectAtIndex:indexPath.row];
    cell.textLabel.text=user_friend.name;

    if([arr_selectedCells containsObject:user_friend.name])
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

didSelectRowAtIndexPath方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserFriend *user_friend=[arr_userFriends objectAtIndex:indexPath.row];
    if([arr_selectedCells containsObject:user_friend.name])
    {
        [arr_selectedCells removeObject:user_friend.name];
    }
    else
    {
        [arr_selectedCells addObject:user_friend.name];
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];        
}

答案 1 :(得分:0)

试试这些

tableView.allowsSelection=YES;
tableView.allowsSelectionDuringEditing = YES;