0
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *checkMarkCellIdentifier =  @"CheckMarkCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:checkMarkCellIdentifier];
    if(cell == nil)
    {
        cell    =   [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:checkMarkCellIdentifier];
    }
      NSUInteger row = [indexPath row];
      NSUInteger oldRow = [self.lastIndexPath row];
     cell.textLabel.text = [self.list objectAtIndex:row];
    cell.accessoryType = (row == oldRow && self.lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;

}


#pragma Mark -
#pragma Mark  Table Delegate Methods



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     self.tickList = [[NSMutableArray alloc]init];
    int newRow = [indexPath row];
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    if(newCell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        self.lastIndexPath = indexPath;
    }
    int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1;

    if(newRow == oldRow)
    {
         UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
       if(oldCell.accessoryType == UITableViewCellAccessoryCheckmark)
       {
           oldCell.accessoryType = UITableViewCellAccessoryNone;
           self.lastIndexPath = nil;
           [self.tickList removeObject:indexPath];

       }
       else{
           oldCell.accessoryType = UITableViewCellAccessoryCheckmark;
           self.lastIndexPath = indexPath;
           [self.tickList addObject:self.lastIndexPath];

       }

    }

    else if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.lastIndexPath = indexPath;
        [self.tickList addObject:self.lastIndexPath];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

从上面的代码中,我什至每次都使用这一行 [self.tickList addObject:self.lastIndexPath] 将选定的单元格存储在名为 tickList 的可变数组中,并且我也使用 removeObject 进行删除,但它对我不起作用。我得到了错误,因为

2013-07-26 11:06:30.339 NAV[762:c07] Application windows are expected to have a root view controller at the end of application launch
2013-07-26 11:06:32.117 NAV[762:c07] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x80343a0
2013-07-26 11:06:32.118 NAV[762:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x80343a0'
*** First throw call stack:
(0x1c9d012 0x10dae7e 0x1d284bd 0x1c8cbbc 0x1c8c94e 0x5a3c 0xce285 0xce4ed 0xad85b3 0x1c5c376 0x1c5be06 0x1c43a82 0x1c42f44 0x1c42e1b 0x1bf77e3 0x1bf7668 0x1effc 0x264d 0x2575)
libc++abi.dylib: terminate called throwing an exception
4

3 回答 3

2

上面代码的替代方法是我在下面给出的。首先我发现它很复杂,但现在它非常简单。上面代码的替代代码是

#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *checkMarkCellIdentifier =  @"CheckMarkCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:checkMarkCellIdentifier];
    if(cell == nil)
    {
        cell    =   [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:checkMarkCellIdentifier];
    }
      NSUInteger row = [indexPath row];
     // NSUInteger oldRow = [self.lastIndexPath row];
     cell.textLabel.text = [self.list objectAtIndex:row];
   for(id key in self.tickList)
{
   if( key == indexPath)
   {
       if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
       {
           cell.accessoryType = UITableViewCellAccessoryNone;
        }
       else
       {
           cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }

   }
}
    //cell.accessoryType = (row == oldRow && self.lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    NSLog(@"%@",indexPath);
    return cell;

}


#pragma Mark -
#pragma Mark  Table Delegate Methods



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
            cell.accessoryType = UITableViewCellAccessoryNone;
        [self.tickList removeObject:indexPath];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.tickList addObject:indexPath];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

在这里,我仅在 tableview 委托方法中进行了更改。我们在这里进行了多个刻度,因此我们不需要创建两个单元格。

选择3行后 在此处输入图像描述

取消选择第二个选定行后

在此处输入图像描述

于 2013-07-26T06:27:37.453 回答
2

您收到此错误是因为您试图将对象插入到 NSArray 指向此行。

-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x80343a0

addObject 不是 NSArray 而不是 NSMutableArray 的方法

请让您的数组NSMutableArray在运行时添加或删除某些项目,因为 NSArray 总是在编译时而不是运行时初始化项目。

希望这可以帮助。

于 2013-07-26T05:48:38.613 回答
0

您可以将 indexpath 存储在数组中,然后在重新加载单元格时使用它。您应该将索引路径存储在

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

    mycustomecell *cell = (mycustomecell *)[tableView cellForRowAtIndexPath:indexPath];
    if ([arrayOfSelectedIndex containsObject:indexPath]) 
            {

                [arrayOfSelectedIndex removeObject:indexPath];


                cell.imgView.image = [UIImage imageNamed:@"BG_Cell-Right_UnChacked.png"];
                cell.backgroundView.backgroundColor=[UIColor clearColor];


            }
            else
            {
                [arrayOfSelectedIndex addObject:indexPath];



                cell.imgView.image = [UIImage imageNamed:@"BG_Cell-Right_Chacked.png"];
                cell.backgroundView.backgroundColor=[UIColor clearColor];


            }
}
于 2013-07-26T13:24:48.663 回答