UITableview Row删除iOS

时间:2013-10-14 13:07:47

标签: ios uitableview

我面临从表视图中删除行的问题 我在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];
}

1 个答案:

答案 0 :(得分:1)

您应首先删除模型中的对象,然后调用deleteRowsAtIndexPaths:withRowAnimation:

您无需致电-reloadData

-(void)unFavouriteRowAtIndexPath:(NSIndexPath *)indexPath
{
    [DatabaseFiles DeleteData:[FavviewArray objectAtIndex:indexPath.row+1 ]];
    [mytableview deleteRowsAtIndexPaths:@[indexPath] 
                       withRowAnimation:UITableViewRowAnimationFade];
}