我有一个tableView,我选择单元格并在数组中添加数据,我也可以选择滑动然后删除一个特定的单元格,最终从数组中删除数据。
问题是,一旦删除了一行,我在重新加载表后就会失去所有选择状态,
为此,我再次使用选择数组检查并重新选择了所有这些单元格,
但是我被困在一个地方,在我真正删除一个单元格并重新加载tableView之前,一旦我在单元格上滑动,所有其他单元格的选择状态也会消失。
注意:我有两个数组,一个包含要在tableView中显示的主题列表,另一个包含所选项目。
以下是一些代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.contactList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell.textLabel setTextColor:[UIColor colorWithRed:103.0/255.0 green:103.0/255.0 blue:103.0/255.0 alpha:1.0]];
[cell.textLabel setFont:[UIFont fontWithName:@"ITCAvantGardeStd-Bk" size:14.0]];
if (![[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"nickName"] isEqualToString:@""])
cell.textLabel.text = [NSString stringWithFormat:@"%@",[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"nickName"]];
else
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"firstName"],[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"lastName"]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Selected cell index==>%d\n",indexPath.row);
//NSString *emailID = [NSString stringWithFormat:@"%@",[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"email_key"]];
NSLog(@"emailID==>%@\n",[self.contactList objectAtIndex:indexPath.row]);
[self.emailShareList addObject:[self.contactList objectAtIndex:indexPath.row]];
//[self.emailShareList insertObject:emailID atIndex:indexPath.row];
NSLog(@"Array value==>%@\n",self.emailShareList);
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"deSelected cell index==>%d\n",indexPath.row);
NSString *emailID = [NSString stringWithFormat:@"%@",[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"email_key"]];
NSLog(@"emailID==>%@\n",emailID);
[self.emailShareList removeObject:[self.contactList objectAtIndex:indexPath.row]];
NSLog(@"deSelect row Array value==>%@\n",self.emailShareList);
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
if(indexPath.row != 0)
{
NSString *contactID = [[self.contactList objectAtIndex:indexPath.row] objectForKey:@"contactId"];
NSLog(@"content on delete row==>%@\n",contactID);
[self.contactList removeObjectAtIndex:indexPath.row];
[self deleteContactToServer:contactID];
}
}
[contactTableView reloadData];
for (int i = 0; i < [self.emailShareList count]; i++)
{
for (int j = 0; j < [self.contactList count]; j++)
{
if([[[self.contactList objectAtIndex:j] valueForKey:@"email"] isEqualToString: [[self.emailShareList objectAtIndex:i] valueForKey:@"email"]])
{
NSIndexPath *path1 = [NSIndexPath indexPathForRow:j inSection:0];
[contactTableView selectRowAtIndexPath:path1 animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellEditingStyle style = UITableViewCellEditingStyleNone;
if(indexPath.row != 0)
style = UITableViewCellEditingStyleDelete;
return style;
}
答案 0 :(得分:4)
删除项目时,无需重新加载整个tableview。您可以使用 - deleteRowsAtIndexPaths:withRowAnimation:方法来删除有问题的单元格(以及相应的模型更新)。这可能会保留您的选择。
要在进入编辑模式时保留您的选择(滑动以进行删除也是编辑模式的一种特殊情况),您需要做两件事:
首先,在tableView上启用allowSelectionDuringEditing:
self.tableView.allowsSelectionDuringEditing = YES;
其次,创建一个UITableView子类并覆盖setEditing:animated:像这样:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSArray *indexPaths = self.indexPathsForSelectedRows;
[super setEditing:editing animated:animated];
for (NSIndexPath *ip in indexPaths) {
[self selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
就个人而言,当从模型的角度来看选择很重要时,我宁愿使用某种自定义选择机制。我将创建一个自定义单元子类,添加一个选择属性,让它相应地更改单元格样式。影响常规表视图选择的内置功能不会导致此类方法出现问题。
答案 1 :(得分:0)
这是另一种保留表格选择进出编辑模式而无需继承UITableView的方法。将以下内容添加到UITableViewControllerView。
在viewDidLoad中添加:
self.tableView.allowsSelectionDuringEditing = YES;
然后覆盖setEditing:animated:
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
[super setEditing:editing animated:animate];
for (NSIndexPath *selectedIndexPath in selectedIndexPaths) {
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}