2013-10-14 159 views
0

我面臨的一個問題,從表視圖刪除一行 我在一個UITableViewCell 使用自定義按鈕,當我點擊該按鈕的應用程序崩潰UITableView的行刪除的iOS

我的代碼如下

- (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]; 
} 
+2

您應該添加應用程序報告的崩潰日誌。 –

+1

首先添加'[cell.accessoryView addSubView:UnFavbtn];'並且爲了您的信息,您的tableView沒有可重用的單元格功能,因爲您在'cellForRowAtIndexPath'方法中對if條件發表了評論,因此對於內存管理來說非常糟糕。 – iPatel

+0

@iPatel: - 感謝Mr.Patel ..其實這個項目就是那個代碼的崩潰Becoze這就是說yi已經評論過,獲得精確的問題的代碼... – Saytovishal

回答

1

您應該刪除的對象模型中的第一個,然後調用deleteRowsAtIndexPaths:withRowAnimation:

而你並不需要調用-reloadData

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

@ Mr.Moxy ...謝謝你這麼多..我會試試看吧現在.... – Saytovishal