Objective c didSelectRowAtIndexPath一次选择多行

Objective c didSelectRowAtIndexPath一次选择多行,objective-c,cocoa-touch,ios,uitableview,Objective C,Cocoa Touch,Ios,Uitableview,我有一个UITableView,里面有27行。我正在尝试更改所选单元格的accessoryType。我在didSelectRowAtIndexPath:delegate方法中这样做 我面临的问题是,当选择一行并更改单元格的accessoryType时,该行的第十一行也会被修改 我尝试打印[indexPath行]值,但它只显示所选的行,而不显示另一行 我真的被这些东西弄糊涂了;请帮帮我 添加了代码cellForRowAtIndexPath方法 UITableViewCell *cell; if (

我有一个
UITableView
,里面有27行。我正在尝试更改所选单元格的
accessoryType
。我在
didSelectRowAtIndexPath:
delegate方法中这样做

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

我尝试打印
[indexPath行]
值,但它只显示所选的行,而不显示另一行

我真的被这些东西弄糊涂了;请帮帮我

添加了代码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行创建新对象

我的
didselectrowatinexpath
methos代码为

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;
        }
    }
}
UITableViewCell*cell=[tableView cellforrowatinexpath:indexPath];
if(cell.accessoryType==UITableViewCellAccessoryCheckmark){
如果([indexPath行]!=0){
NSIndexPath*tempIndex=[NSIndexPath indexPathForRow:0第1节:0];
UITableViewCell*tempCell=[tableView cellForRowAtIndexPath:tempIndex];
tempCell.accessoryType=UITableViewCellAccessoryNone;
}
cell.accessoryType=UITableViewCellAccessoryNone;
}
否则{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
如果([indexPath行]==0){
对于(int i=0;i<[数据源计数];i++){
NSIndexPath*tempIndex=[NSIndexPath indexPathForRow:i+1第1节:0];
UITableViewCell*tempCell=[tableView cellForRowAtIndexPath:tempIndex];
if(cell.accessoryType==UITableViewCellAccessoryCheckmark){
tempCell.accessoryType=UITableViewCellAccessoryCheckmark;
}
否则{
tempCell.accessoryType=UITableViewCellAccessoryNone;
}
}
}
请帮助我在多选或任何其他方式来解决多选的问题


提前谢谢

这里有一种方法:

- (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;
}
请记住,表视图中的每个单元格实际上都是重复使用的同一对象。如果您没有在每次调用CellForRowatineXpath时设置附件类型,那么当新单元格滚动到屏幕上时,它们都将具有相同的附件

多选

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

您的第一个选择:未记录的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
- (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子类包含一个数组,该数组指示选择了哪些单元格。然后在cellForRowAtIndexPath中,使用该数组配置单元格的外观。然后,您的didSelectRowAtIndexPath方法应该如下所示:

nm /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/Frameworks/UIKit.framework/UIKit
- (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子类中创建InExpathisselected、RemoveInExpathFromSelection和AddInExpathToSelection方法。这些方法应该完全按照它们的名称执行:添加、删除和检查数组中的索引路径。如果使用此选项,则不需要DidDecleRowatineXpath实现

请记住,表视图中的每个单元格实际上都是重复使用的同一对象。如果您没有在每次调用CellForRowatineXpath时设置附件类型,那么当新的单元格滚动到屏幕上时,它们都将具有相同的附件。“-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

希望这有帮助!

发布您的代码可能是什么?很可能您在cellForRowAtIndexPath方法中错误地重用了单元格在tableview代码中发布您的cellforrowindex也请检查example@ll感谢您的回复,我已经添加了代码,请查看您是否能提供帮助…谢谢您的提醒,如果仅y单选完成。如果我需要实现多选,我应该做些什么改变?感谢我们做了一些测试,并对多选部分进行了大量修改(未记录的API内容)。嘿,谢谢你为帮助我所做的努力,但我仍然没有解决问题。我已经编辑了我的问题并添加了代码。请查看这是否有助于你指导我。谢谢:)