UITableView多选。选择行自动选择随机行

时间:2012-07-25 12:02:55

标签: iphone ios xcode uitableview

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

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

当我滚动桌面视图时,它会自动更改选择。

我使用的代码行数较少(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 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

检查/取消选中附件类型的单元格的逻辑应该转到cellForRowAtindexPath。在didSelectRowAtIndexPath中,您应该只处理选定的行。

答案 2 :(得分:1)

在.h文件中添加一个mutablearray。在tableDataSourc数组中添加尽可能多的对象到数组。现在我的意思是,如果它包含0,那么该单元格未被选中或者它包含1则则选择该单元格

现在这样做,因为你有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中选择了值

答案 3 :(得分:0)

嗯......今天早上我还在喝第一杯咖啡,但有些东西告诉我,当调用didSelectRowAtIndexPath时,你已经选择了“行”。然后似乎if语句可能正在做一些时髦的事情,并选择下一行或其他东西。你有没有尝试评论if语句?我知道可能一次只能选择一个,但只是看看是否是原因。另外,它是下一行被选中的那个吗?

相关问题