didSelectRowAtIndexPath一次选择多行

时间:2011-05-19 11:19:17

标签: objective-c cocoa-touch ios uitableview

我有一个UITableView填充了27行。我正在尝试更改所选单元格的accessoryType。我在didSelectRowAtIndexPath:委托方法中执行此操作。

我面临的问题是,当选择行并更改单元格的accessoryType时,该行的第11行也会被修改。

我已尝试打印[indexPath row]值,但它只显示已选择的行而非另一行。

我对这些东西感到很困惑;请帮帮我。

添加代码 cellForRowAtIndexPath方法

UITableViewCell *cell;
if ([indexPath row] == 0) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"acell"];
}
else {
    cell = [tableView dequeueReusableCellWithIdentifier:@"bcell"];
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

if (cell == nil && [indexPath row] != 0) {
    cell = [[[UITableViewCell alloc] initWithStyle:
             UITableViewCellStyleSubtitle reuseIdentifier:@"bcell"] autorelease];
}
else if(cell == nil && [indexPath row] == 0){
    cell = [[[UITableViewCell alloc] initWithStyle:
             UITableViewCellStyleSubtitle reuseIdentifier:@"acell"] autorelease];
}

if ([cell.contentView subviews]){
    for (UIView *subview in [cell.contentView subviews]) {
        [subview removeFromSuperview];
    }
}
if ([indexPath row] == 0) {
    cell.textLabel.text = @"Select All";
    cell.textLabel.font = [UIFont boldSystemFontOfSize:13.0f];
}
else {
    cell.textLabel.text = @"Some Text Here"
    cell.detailTextLabel.text = @"Another piece of text here"   
}
return cell;

我正在做%10因为行为在第11行之后重复,因此尝试为每第11行创建新对象。

我的didSelectRowAtIndexPath方法代码是

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    if ([indexPath row] != 0) {
        NSIndexPath *tempIndex = [NSIndexPath indexPathForRow:0 inSection:0];
        UITableViewCell *tempCell = [tableView cellForRowAtIndexPath:tempIndex];
        tempCell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
}
else{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
if ([indexPath row] == 0) {
    for (int i = 0; i < [dataSource count]; i++) {
        NSIndexPath *tempIndex = [NSIndexPath indexPathForRow:i+1 inSection:0];
        UITableViewCell *tempCell = [tableView cellForRowAtIndexPath:tempIndex];
        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            tempCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else{
            tempCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
}

请帮我多种选择或其他任何方式解决多项选择问题。

提前致谢!!

2 个答案:

答案 0 :(得分:15)

这是一种方法:

- (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];        
    }

    [cell.textLabel setText:[NSString stringWithFormat:@"Row %d", indexPath.row]];

    NSIndexPath* selection = [tableView indexPathForSelectedRow];
    if (selection && selection.row == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;  
    }

    // Configure the cell.
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

请记住,表视图中的每个单元实际上都是重用的对象。如果每次调用cellForRowAtIndexPath时都没有设置附件类型,当新单元格滚动到屏幕上时,它们将具有相同的附件。

多选

对于多重选择,它有点复杂。

您的第一个选择:未记录的API

请注意,这仅在表格处于编辑模式时才有效。将每个单元格的编辑样式设置为未记录的UITableViewCellEditingStyleMultiSelect。完成后,您可以通过UITableView的未记录成员获取表视图的选择:indexPathsForSelectedRows。那应该返回一个选定单元格的数组。

您可以通过将其置于标题中来公开此功能:

enum {
    UITableViewCellEditingStyleMultiSelect = 3,
};

@interface UITableView (undocumented)
- (NSArray *)indexPathsForSelectedRows;
@end

然后为每个单元格设置编辑样式:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleMultiSelect;
}

当表处于编辑模式时,您将在单元格上看到多选控件。

要查看其他未记录的API,您可以使用nm命令行实用程序,如下所示:

nm /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/Frameworks/UIKit.framework/UIKit

您的第二个选择:自行管理选择

让您的UITableView子类包含一个数组,指示选择了哪些单元格。然后在cellForRowAtIndexPath中,使用该数组配置单元格的外观。你的didSelectRowAtIndexPath方法应该是这样的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView indexPathIsSelected:indexPath]) {
        [tableView removeIndexPathFromSelection:indexPath];
    } else {
        [tableView addIndexPathToSelection:indexPath];
    }
    // Update the cell's appearance somewhere here
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

这假设您在UITableView子类中创建indexPathIsSelected,removeIndexPathFromSelection和addIndexPathToSelection方法。这些方法应该完全符合它们的名称所暗示的:添加,删除和检查数组中的索引路径。如果使用此选项,则不需要didDeselectRowAtIndexPath实现。

答案 1 :(得分:0)

  

请记住,表视图中的每个单元实际上都是重用的对象。如果每次调用cellForRowAtIndexPath时都没有设置附件类型,当新单元格滚动到屏幕上时,它们将具有相同的附件。“ - daxnitro

这是我被抓住的地方。我有我的设置,以便在我的“cellForRowAtIndexPath”函数中,我只会更改我的已检查单元格数组中指定的附件,当我应该做的是更新表中所有单元格的附件。

换句话说:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//normal set up

    //retrieve key
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    id obj = [settings objectForKey:@yourKey];

    //if the array is not populated, keep standard format for all cells
    if (obj == nil){
        selectedStyles = [[NSMutableArray alloc] initWithObjects:nil];
        [cell setAccessoryType:UITableViewCellAccessoryNone];  //no check mark
        [cell textLabel].textColor = [[UIColor alloc] initWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:1.0];  //keep black color

    }
    //else retrieve information from the array and update the cell's accessory
    else{
        //if the cell is in your array, add a checkbox
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];  //add check box
        [cell textLabel].textColor = [[UIColor alloc] initWithRed:50.0/255 green:79.0/255 blue:133.0/255 alpha:1.0];  //change color of text label
        //if the cell is not in your array, then keep standard format
        [cell setAccessoryType:UITableViewCellAccessoryNone];  //no check mark
        [cell textLabel].textColor = [[UIColor alloc] initWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:1.0];  //keep black color

希望这有帮助!

相关问题