tableView 刪除小技巧


//滑動刪除
-( void )tableView:( UITableView *)tableView commitEditingStyle :(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath :( NSIndexPath *)indexPath
{  
     NSUInteger row = [indexPath row ];
     [bookInforemoveObjectAtIndex:row]; //bookInfo為當前table中顯示的array
     [tableView deleteRowsAtIndexPaths :[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
 
/*此時刪除按鈕為Delete,如果想顯示為“刪除” 中文的話,則需要實現
UITableViewDelegate 中的- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath方法*/
 
- ( NSString *)tableView:( UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath :( NSIndexPath *)indexPath{   
     return @"刪除" ;   
}   
//或者,最簡單的方式,將plist中的Localization native development region改為China即可
 
//這只是讓默認的Delete按鈕顯示成了中文的刪除按鈕而已,如果想將這個刪除按鈕換成其他圖片形式的,怎么辦呢?
-( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath :( NSIndexPath *)indexPath   
{   
     static NSString * RootViewControllerCell = @"RootViewControllerCell" ;   
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :RootViewControllerCell];   
     if (cell == nil )   
     {   
         cell = [[[ UITableViewCell alloc ] initWithFrame : CGRectZero reuseIdentifier :RootViewControllerCell]autorelease];   
             
         UIButton *button = [ UIButton buttonWithType :UIButtonTypeCustom];   
         [button setBackgroundImage :[ UIImage imageNamed : @"delete.png" ] forState :UIControlStateNormal];   
         [button setFrame :CGRectMake( 2 8 0 , 1 0 , 3 0 , 3 0 )];   
         [button addTarget : self action : @selector (del:) forControlEvents :UIControlEventTouchUpInside];   
         [cell .contentView addSubview :button];          
     }   
         
     cell .textLabel .text = [array objectAtIndex :[indexPath row ]];   
     cell .tag = [indexPath row ];   
         
     NSArray *subviews = [cell .contentView subviews ];   
     for ( id view in subviews)   
     {   
         if ([view isKindOfClass :[ UIButton class ]])   
         {   
             [view setTag :[indexPath row ]];   
             [cell .contentView bringSubviewToFront :view];   
         }   
     }   
     return cell;   
}   
     
-( void )del:( UIButton *)button   
{   
     NSArray *visiblecells = [ self .table visibleCells ];   
     for ( UITableViewCell *cell in visiblecells)   
     {   
         if (cell .tag == button .tag )   
         {   
             [array removeObjectAtIndex :[cell tag ]];   
             [table reloadData ];   
             break;   
         }   
     }   
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM