2012-07-25 84 views
1

我在可用视图中有30行,我试图选择多行并将它们设置为检查。UITableView多选。自动选择行随机选择行

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

另外,当我滚动tableview它会自动改变选择。

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

回答

1

在.h文件中添加一个可变阵列。这里添加与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中被选中

+0

嗨,所以selectedIndexes是你希望我声明的另一个可变数组,还是应该添加一个比selectedIndexes更多的数组? – 2012-07-25 13:28:15

+0

你不需要添加任何其他可变数组,因为我假设你有selectedIndexes数组,如果没有,然后添加可变数组和alloc在viewDidLoad方法 – 2012-07-26 04:36:00

0

嗯...我仍然在今天上午的咖啡我的第一杯,但东西告诉我你已经有行“选择”时,didSelectRowAtIndexPath方法被调用。然后,似乎if语句可能会做些时髦的事情,并选择下一行或其他东西。你有没有试过评论if语句?我知道可能一次只能选一个,但只是为了看看是否是原因。另外,是否是之前选择的下一行?

+0

我试过评论。没有预选的行。没有它不是以前选择的行。另外,当我向下滚动或向上滚动时,选择会自动更改,然后返回以查看选择。我认为我的逻辑存在一个重大缺陷。如果你可以建议一些其他的逻辑或代码也可以。 我想要做的是,在我的表格中有30行,并且能够从30中选择最多3行,并将检查应用于所选行并将检查行存储在一个字符串变量中。 – 2012-07-25 12:19:02

2

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

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

+0

好的,你的意思是我应该将索引存储在一个数组中,然后使用该数组将附件更改为打开或关闭?你能证明使用代码块吗? – 2012-07-25 12:20:55

+0

或者如果你可以在我的帖子中编辑它会很棒。谢谢你,对于麻烦抱歉,我是xcode的新手。 – 2012-07-25 12:31:33

2

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