#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
答案 0 :(得分:2)
您收到此错误是因为您尝试将对象插入指向此行的NSArray。
-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x80343a0
addObject不是NSArray的方法而不是NSMutableArray
请让您的数组
NSMutableArray
在运行时添加或删除某些项,因为NSArray始终在编译时而不是运行时使用项初始化。
希望这有帮助。
答案 1 :(得分: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行后取消选择第二个选定的行后
答案 2 :(得分: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];
}
}