我想在uitablecell中加载数据时选中数据。我知道将其放在didselect部分中时可以使用,但是我们可以在cellForRowAtIndexPath中使用它吗?如果是,如何使用?我已经尝试过了,但是没有显示任何选中标记。

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

AddGoalsTableViewCell *cell = (AddGoalsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AddGoalsTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
cell.addGoalsText.font = [UIFont fontWithName:@"AvenirNext-Medium" size:17];
cell.addGoalsText.textColor = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f];
    cell.addGoalsText.text = [[goalsArray objectAtIndex:indexPath.row] valueForKey:@"name"];
    PFObject *currentUser = [[goalsArray objectAtIndex:indexPath.row] valueForKey:@"user"];
    User *user = [[User alloc] initWithUserId:[currentUser valueForKey:@"userID"] userName:[currentUser valueForKey:@"name"] anduserpassword:[currentUser valueForKey:@"password"]];

    if(user==nil)
    {

    }
    else if([[user userId]isEqualToString:[[NSString stringWithFormat:@"%@", [[[UIDevice currentDevice] identifierForVendor] UUIDString] ] lowercaseString]])
    {
        [self.tableView beginUpdates];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;  <------Here
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];
    }


return cell;
}

最佳答案

请参考以下代码

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

    if(cell == nil )
    {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.lastIndexPath = indexPath;

    [tableView reloadData];
}


并且lastIndexPath是一个属性(保留)NSIndexPath * lastIndexPath;

关于ios - 在ios中的单元格中加载数据时,在uitableView中设置选中标记,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30231492/

10-10 20:58
查看更多