質問

アルファベット順にセクションと行にインデックス付けされたユーザー名のリストを含むTableViewがあります。セクション内の行の1つをタップすると、正しいユーザーが受信者の配列に追加され、チェックマークは名前以外のセル内の場所です。しかし、チェックマークは選択されていない他のユーザー名の横に表示されます。受信者の配列にはありません。選択したセルを新しいインデックスパスで再割り当てしようとしました(以下のコードを参照)が、動作させることができませんでした。正しいパスを登録しますが、割り当てません。私は、各セクションの行をユーザーに割り当てるために同様の方法を使用していますが、何らかの理由でアクセサリーマークが私に問題を与えています。私はこの同じトピックについてOverflowで他のいくつかのスレッドを見てきましたが、私のケースの解決策に至ることができます。手がかりはありますか?乾杯!

 - (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です:

- (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で2つの間違いを犯しました。これは、セルがnullであるかどうかを確認して、レシピエントリストに従ってセルのアクセサリタイプを設定しました。

以下をする必要があります:

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