gpt4 book ai didi

iphone - UITableView 多选。使用所选行自动选择随机行

转载 作者:行者123 更新时间:2023-12-01 17:59:56 25 4
gpt4 key购买 nike

我在 uitable View 中有 30 行,我正在尝试选择多行并将它们设置为选中。

每当我选择一行时,会自动选择另一行并检查所需的行。

此外,当我滚动表格 View 时,它会自动更改选择。

我使用的代码行数较少(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);
}

最佳答案

单元格被缓存并重复使用,因此如果您将附件设置为一个,然后将其用于表格中的不同位置,它仍然具有附件。

最好只跟踪 didSelectRowAtIndexPath: 中选择了哪些单元格作为数据。方法,然后为 cellForRowAtIndexPath: 中的所有单元打开或关闭附件.

关于iphone - UITableView 多选。使用所选行自动选择随机行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11649235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com