我面临从表格视图中删除一行的问题我在 UITableviewCell 中使用自定义按钮当我点击该按钮时,应用程序崩溃
我的代码在下面
- (void)viewDidLoad
{
[DatabaseFiles check_Create_DB];
FavviewArray = [DatabaseFiles getData];
FavviewArray = [FavviewArray valueForKey:@"Fav_Msg"];
[mytableview setDataSource:self];
[mytableview setDelegate:self];
}
-(void)viewWillAppear:(BOOL)animated
{
//============Custome Favourite Button================
UIImage *image = [UIImage imageNamed:@"FAv_On-iphone.png"];
UnFavbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
[UIButton buttonWithType:UIButtonTypeCustom];
[UnFavbtn setBackgroundImage:image forState:UIControlStateNormal];
UnFavbtn.backgroundColor = [UIColor clearColor];
//cell.accessoryView = button;
[UnFavbtn addTarget:self action:@selector(UnFavouriteClick) forControlEvents:UIControlEventTouchUpInside];
//==================================================
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [FavviewArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// }
cell.textLabel.text = [FavviewArray objectAtIndex:indexPath.row];
cell.accessoryView =UnFavbtn;
//[self.mytableview reloadData];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedrow = (indexPath.row);
NSLog(@"Selected Row====%d",lastindexpath.row);
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing)
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
lastindexpath = indexPath;
[self UnFavouriteClick];
// [FavviewArray removeObjectAtIndex:lastindexpath.row];
// [mytableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:lastindexpath] withRowAnimation:UITableViewRowAnimationFade];
//[DatabaseOperation DeleteData:[Arrayid objectAtIndex:lastIndexPath.row+1 ]];
//[mytableview reloadData];
}
}
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Button-iphone.png"]];
}
- (void)UnFavouriteClick
{
NSLog(@"Last Indexpath====%d",lastindexpath.row+1);
//[FavviewArray removeObject:selectedrow];
//[FavviewArray removeObjectAtIndex:lastindexpath.row];
[mytableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:lastindexpath] withRowAnimation:UITableViewRowAnimationFade];
[DatabaseFiles DeleteData:[FavviewArray objectAtIndex:lastindexpath.row+1 ]];
[mytableview reloadData];
}