didSelectRowAtIndexPath 选择多个 tableView 单元格附件 [英] didSelectRowAtIndexPath selecting multiple tableView cell accessory's

查看:44
本文介绍了didSelectRowAtIndexPath 选择多个 tableView 单元格附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tableView,其中包含按字母顺序索引到部分和行中的用户名列表.当我点击某个部分中的某一行时,正确的用户会添加到我的收件人数组中,并且在单元格中除了他们的姓名外还会有一个复选标记......但其他尚未选择的用户名旁边也会显示一个复选标记,并且不在收件人数组中.我试图用新的 indexPath(见下面的代码)重新分配选定的单元格,但无法让它工作.它注册了正确的路径,但不会分配它.我正在使用类似的方法为用户分配每个部分中的行,没有问题,但由于某种原因,附件标记给我带来了问题.我已经看到其他一些关于同一主题的溢出线程,但是洗;无法为我的案例找到解决方案.任何线索?干杯!

I have a tableView containing a list of user names that are indexed into sections and rows alphabetically. When I tap on one of the rows in a section, the correct user is added to my recipients array and a checkmark is places in the cell besides their name.. but a checkmark is also displayed next to other usernames that have not been selected and are not in the recipients array. I have tried to reassign the selected cell with a new indexPath (see code below) but have not been able to get it to work. It registers the correct path, but won't assign it. I am using a similar method for assigning the users the the rows in each section with no trouble but for some reason the accessory marks are giving me problems. I've seen some other threads on overflow about this same topic but wash;'t able to come to a solution for my case. any clues? cheers!

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

    int row = indexPath.row;
    int section = indexPath.section;
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];

    [tableView deselectRowAtIndexPath:newIndexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];

    NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
    PFUser *user = [array objectAtIndex:indexPath.row];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user];
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
    }];

这里是 cellForRowAtIndexPath:

and here's the cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Get the user names from the array associated with the section index in the sections array.
    NSArray *userNamesInSection = (self.sectionsArray)[indexPath.section];

    // Configure the cell with user name.
    UserNameWrapper *userName = userNamesInSection[indexPath.row];
    cell.textLabel.text = userName.user;

    return cell;
}

推荐答案

我看到你在 CellForRowAtIndexPath 中犯了两个错误,没有检查单元格是否为空来创建一个并根据收件人列表为单元格设置附件类型.

As I see you made 2 mistakes in CellForRowAtIndexPath that did not check if cell is null to create one and set accessoryType for cell according to the recipient list.

你应该像下面这样:

NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

PFUser *user = [self getUserAtIndexPath:indexPath];
cell.textLabel.text = user.name;

if ([self.recipients containObject:user]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;

这篇关于didSelectRowAtIndexPath 选择多个 tableView 单元格附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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