删除单元格并重新加载表视图时访问错误 [英] Getting bad access while deleting cell and reloading table view
本文介绍了删除单元格并重新加载表视图时访问错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用自定义UITableViewCell构建一个表,但在删除单元格后,当我尝试加载视图时,我收到一个EXC_BAD_ACCESS错误,这里我从字典中获取数据并将其加载到表视图。
I am trying to build a table using custom UITableViewCell, but after deleting cell and when I try load the view, I get an EXC_BAD_ACCESS error,here i'm getting data from a dictionary and loading it to table view.
我的表格代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = (uploadCustomCell *)[tabelView1 dequeueReusableCellWithIdentifier:@"cell"];
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:@"uploadCustomCell" owner:self options:nil];
cell = (uploadCustomCell *)self.uploadCustomcell;
}
saveBtnCcell.hidden = YES;
cell.textNamefield.hidden = YES;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.defaultSwitch setEnabled:NO];
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
NSLog(@"dict dict :%@",dictionary);
//
cell.nameLabelCell.text = [dictionary valueForKey:@"VideoName"];
cell.userName.text = [dictionary valueForKey:@"User"];
NSString *defaultVideo = [dictionary valueForKey:@"DefaultVideo"];
if ([defaultVideo isEqualToString:@"1"]) {
[cell.defaultSwitch setOn:YES animated:YES];
}
else {
[cell.defaultSwitch setOn:NO animated:YES];
}
[cell.defaultSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged];
cell.thumbImg.image = [arrayimage objectAtIndex:indexPath.row];
VideoNameTextField.hidden = YES;
return cell;
}
- (void)setState:(id)sender {
state = [sender isOn];
// NSString *rez = state == YES ? @"YES" : @"NO";
NSLog(@"state.........:%d",state);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"height:%f",uploadCustomcell.frame.size.height);
return 207;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
NSLog(@"Dictionary:%@",dictionary);
NSLog(@"indexpath:%@",indexPath);
//
NSLog(@"at index%d obj:%@",indexPath.row,dictionary);
NSString *nameDetails = [dictionary valueForKey:@"VideoName"];
guid = [dictionary valueForKey:@"GUID"];
detailsNameLbl.text = nameDetails;
detailsVehImg.image = [arrayimage objectAtIndex:indexPath.row];;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[contents removeObjectAtIndex:indexPath.row];
[tabelView1 reloadData];
}
}
推荐答案
尝试这个:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
您还必须使用此方法:
You must use this method also:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[contents removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//[tabelView1 reloadData];
}
这篇关于删除单元格并重新加载表视图时访问错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文