1

uitable ビューに 30 行あり、複数の行を選択してチェック済みに設定しようとしています。

1 つの行を選択するたびに、自動的に別の行が選択され、目的の行でチェックされます。

また、テーブルビューをスクロールすると、選択が自動的に変更されます。

このコードを少ない行数 (8) で使用したところ、完全に機能しました。12行以上の場合、私が説明した問題が発生します。

それを機能させるための他のコード/チュートリアルを提案できれば、それも問題ありません。

私はxcodeを初めて使用します。どんな助けも大歓迎です。ありがとうございました。

これが私のコードです:

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [listOfItems count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

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

// Configure the cell...
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;

return cell;
}

#pragma mark -
#pragma mark Table view delegate

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

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {

    if(count < 3)
    {

        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
        count++;
    }

    NSLog(@"Count: %d", count);
    NSLog(@"Items I selected @ %d:", indexPath.row);
    NSLog(@"Items I selected: %@", selectedIndexes);
} 

else {

    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
    count --;
    NSLog(@"Items I de-selected @ %d:", indexPath.row);

}

[tableView deselectRowAtIndexPath:indexPath animated:NO];

NSMutableString *resultlearning = [[NSMutableString alloc]init];
for (int i = 0; i < [listOfItems count]; i++) {
    NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
    //[tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        [resultlearning appendFormat:@"%@, ",cell.textLabel.text];
    }
}
if (resultlearning.length > 2) {
    [resultlearning replaceCharactersInRange:NSMakeRange(resultlearning.length-1, 1) withString:@""];
}
NSLog(@"Result: %@",resultlearning);
}
4

4 に答える 4

2

セルのアクセサリタイプをチェック/チェック解除するロジックは、cellForRowAtindexPathに移動する必要があります。didSelectRowAtIndexPathでは、選択した行のみを処理する必要があります。

于 2012-07-25T12:23:59.533 に答える
2

セルはキャッシュされて再利用されるため、アクセサリを 1 つに設定し、それをテーブル内の別の場所に使用した場合でも、アクセサリは保持されます。

メソッドでどのセルがデータとして選択されているかだけを追跡didSelectRowAtIndexPath:し、 のすべてのセルに対してアクセサリをオンまたはオフにすることをお勧めしcellForRowAtIndexPath:ます。

于 2012-07-25T12:17:59.160 に答える
1

.hファイルに1つのmutablearrayを追加します。ここで、tableDataSourc配列と同じ数のオブジェクトを配列に追加します。私の意図は、0が含まれている場合、そのセルは選択されていないか、1が含まれている場合、そのセルが選択されるということです。

uがselectedIndexes配列を持っているので、これを実行します。別のmutablearrayが必要です。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
for(int i = 0;i<[listOfItems count];i++)
{
[selectedIndexes addObject:@"0"];
}
   return [listOfItems count]
 }


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

 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

 if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {

     [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
     [selectedIndexes replaceObjectAtIndex:indexPath.row withObject:@"1"];
}
else
{          
selectedCell.accessoryType = UITableViewCellAccessoryNone;
[selectedIndexes replaceObjectAtIndex:indexPath.row withObject:@"0"];
}

これで、selectedIndexesに1つの値を持つオブジェクトがあり、値がlistOfItemsで選択されていることを意味します

于 2012-07-25T12:21:31.953 に答える
0

うーん...私はまだ今朝 1 杯目のコーヒーを飲んでいますが、didSelectRowAtIndexPath が呼び出されたときに既に行が "選択" されていることがわかります。次に、if ステートメントが何かファンキーなことをして、次の行または何かを選択しているように見えます。if文をコメントアウトしてみましたか?おそらく一度に 1 つしか選択されないことはわかっていますが、それが原因かどうかを確認するためです。また、選択されているのは次の行、前の行ですか?

于 2012-07-25T12:15:01.923 に答える