[英]Xamarin UICollectionView some cells disappear after scrolling down and back
[英]while dragging Up and Down after selecting some cells my check marks getting disappear
#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
之所以出现此错误,是因为您试图将对象插入此行指向的NSArray。
-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x80343a0
addObject不是NSArray的方法,而不是NSMutableArray
请使您的数组
NSMutableArray
在运行时添加或删除某些项,因为NSArray总是在编译时而不是运行时初始化项。
希望这可以帮助。
上面代码的替代方法是我在下面给出的内容。首先我发现它很复杂,但现在非常简单。上面代码的替代代码是
#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的Delegate方法中进行了更改。我们在这里进行了多次滴答,所以我们不需要创建两个单元格。
选择3行之后
取消选择第二行后
您可以将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];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.