문제

섹션과 행으로 색인 된 사용자 이름 목록이 포함 된 테이블 뷰가 있습니다. 섹션의 행 중 하나를 탭하면 올바른 사용자가 수신자 배열에 추가되고 확인 마크는 이름 외에 셀의 위치입니다. 그러나 선택하지 않은 다른 사용자 이름 옆에 확인 표시가 표시됩니다. 수신자 배열에 없습니다. 선택한 셀을 새로운 색인 경로로 재 할당하려고했지만 (아래 코드 참조) 작동 할 수는 없었습니다. 올바른 경로를 등록하지만 할당하지 않습니다. 각 섹션의 행을 사용자에게 할당하기 위해 비슷한 방법을 사용하고 있지만 어떤 이유로 든 액세서리 마크가 나에게 문제를 일으킨다. 나는이 같은 주제에 대해 오버플로에 대한 다른 스레드를 보았지만 씻을 수 있습니다. 단서가 있습니까? 건배!

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

그리고 여기에 cellforyatindexpath가 있습니다.

- (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;
}
도움이 되었습니까?

해결책

내가 알다시피, 당신이 CellforseAtIndexPath에서 셀이 null인지 확인하지 않은 2 개의 실수를 저지르고 수신자 목록에 따라 셀에 대한 accessoryType를 설정합니다.

아래에서 좋아해야합니다.

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top