didSelectRowAtIndexPath selecting multiple tableView cell accessory's

老子叫甜甜 提交于 2019-12-11 04:17:59

问题


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]);
        }
    }];

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;
}

回答1:


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.

You should do like below:

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;


来源:https://stackoverflow.com/questions/19828171/didselectrowatindexpath-selecting-multiple-tableview-cell-accessorys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!