2012-09-07 57 views
0

這個UITableViews會讓我瘋狂!默認情況下刪除uitableview行的麻煩方法

我已經通過的UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

doneButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 15, 30, 30)]; 
[cell addSubview:doneButton]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
[doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchDown]; 

UILabel *postedTime = [[UILabel alloc]initWithFrame:CGRectMake(240, 4, 60, 15)]; 
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
postedTime.backgroundColor = [UIColor clearColor]; 
[cell addSubview:postedTime]; 
cell.textLabel.textColor = [UIColor blackColor]; 

UILabel *post = [[UILabel alloc] initWithFrame:CGRectMake(45, 25, 190, 30)]; 
[cell addSubview:post]; 

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(45, 4, 180, 15)]; 
[cell addSubview:title]; 

mission *m = (mission *)[missionsArray objectAtIndex:indexPath.row]; 
title.text = m.title; 
post.text = m.post; 
postedTime.text = m.posted; 

.h文件中創建:

IBOutlet UITableView *table; 
NSMutableArray *missionsArray; 

我試圖通過方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [missionsArray removeObjectAtIndex:indexPath.row]; 
     [table reloadData]; 
} 

刪除行,當我點擊 「刪除」按鈕EXC_BAD_ACCESS。我試圖調用[missionsArray removeAllObject]; - 同樣的錯誤。然後我試着NSLog(@「%u」,indexPath.row); - 它打印正確的行(當我選擇第一行時,它返回0等等)。我也試過這個

if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [missionsArray removeObjectAtIndex:indexPath.row]; 
     [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 
     [table reloadData]; 
} 

而且我沒有好的結果。也許細胞高度影響到它... 請幫忙。我不知道該做什麼,在哪裏搜索。


問題解決了。 當我試圖釋放我的任務對象時,麻煩是邏輯錯誤。

+1

與你的問題沒有關係......但是有沒有一個原因,你爲什麼每次「創建」按鈕和額外的標籤,而不是一次? –

+0

也許這是我的邏輯錯誤 – pash3r

+0

,因爲你所有的單元格都是相同的並且是「可重用的」,將所有的init代碼移動到if(cell == nil){'block內部的按鈕和標籤中,這會提高性能,子類保持整潔) –

回答

0

感謝您的幫助。我解決了這個問題。答案在更新的問題中!

1

假設你只有在你TableView 部分試試這個:

if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [missionsArray removeObjectAtIndex:indexPath.row]; 
     [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     // No need to reload the tableview. Remove this line entirely [table reloadData]; 
} 
+0

相同的結果:( – pash3r

2
#import "ViewController.h" 


@implementation ViewController 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

    #pragma mark - View lifecycle 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 
     missionsArray=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C", nil]; 
    } 

    - (void)viewDidUnload 
    { 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
     // e.g. self.myOutlet = nil; 
    } 


    #pragma mark TableView delegate method. 
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1 { 
     return 1; 
    } 


    - (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section 
    { 
     return [missionsArray count]; 
    } 


    - (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

     return 44; 


    } 
    - (UITableViewCell *)tableView:(UITableView *)tableView 
      cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"myCell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 

      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 
     } 

     UIButton * doneButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 15, 30, 30)]; 
     [cell addSubview:doneButton]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchDown]; 

     UILabel *postedTime = [[UILabel alloc]initWithFrame:CGRectMake(240, 4, 60, 15)]; 
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
     postedTime.backgroundColor = [UIColor clearColor]; 
     [cell addSubview:postedTime]; 
     cell.textLabel.textColor = [UIColor blackColor]; 

     UILabel *post = [[UILabel alloc] initWithFrame:CGRectMake(45, 20, 190, 30)]; 

     post.text=[missionsArray objectAtIndex:indexPath.row]; 
     [cell addSubview:post]; 

     UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(45, 4, 180, 15)]; 
     [cell addSubview:title]; 

     return cell; 

    } 

     enter code here 

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      [missionsArray removeObjectAtIndex:indexPath.row]; 
      [table reloadData]; 
     } 
    } 




    @end 
+0

我填充missionarray喜歡在你的答案和我的UItableView有1節。它不工作:\ – pash3r

+0

它的工作一旦檢查它 – 08442

+0

哦對不起,我是盲目的,當時我會檢查它 – pash3r