我有一个 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;
}