2012-03-30 25 views
0

我有一個UITableView的代碼,有兩列我在網上找到。當我向其中添加另一行時,它可以正常工作,但是當我選擇一行來刪除它時,只會刪除最後一行。我只是用[NSMutableArray addObject:]將行添加到數組的底部,但是當我刪除一行時,我首先選擇要刪除的行。刪除行後,UITableView reloadData顯示不正確

這裏選擇行------(我檢查,以確保tableViewIdx是正確的)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    tableViewIdx = [indexPath row]; 
} 

然後刪除按鈕------(我檢查,以確保對象實際上從陣列中移除)

-(IBAction)bt_DeletePressed:(id)sender{ 
    [mutableArray removeObjectAtIndex:tableViewIdx]; 
    //delete actual content 
    for(int i = VStableViewIdx;i<10;i++){ 
     g_vitals[i] = g_vitals[i+1]; 
    } 
    [TableView reloadData]; 
    tableViewIdx--; 
} 

和部分的tableView代碼

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    int count = [listData count]; 
    if(self.editing) count++; 
    tableSize = count; 
    return count; 
} 

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

    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row]; 

    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil){ 
     cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 

     NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
     [formatter setDateFormat:@"HH:mm"]; 

     UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 60.00,tableView.rowHeight)] autorelease]; 
     [cell addColumn:60]; 
     label.font = [UIFont systemFontOfSize:12.0]; 
     label.text = textView.text; 
     [formatter release]; 
     label.textAlignment = UITextAlignmentRight; 
     label.textColor = [UIColor blueColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:label]; 

     label = [[[UILabel alloc] initWithFrame:CGRectMake(70.0, 0, 60.0,tableView.rowHeight)] autorelease]; 
     [cell addColumn:500]; 
     label.font = [UIFont systemFontOfSize:12.0]; 
     label.text = [NSString stringWithFormat:@"%d",respRateTemp]; 
     label.textAlignment = UITextAlignmentRight; 
     label.textColor = [UIColor blueColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:label]; 
    } 

    return cell; 
} 

任何想法,爲什麼它WOU ld是刪除最後一行,而不是所選的一行?

編輯︰我實際上能夠通過使用deleteRowsAtIndexPaths自己修復它:而不是updateData,但現在我有另一個問題。我的程序在模擬器上運行良好,但經常在設備上崩潰。它出現錯誤:

編程接收信號:「EXC_BAD_ACCESS」。

數據格式化程序暫時不可用,將在'繼續'後重新嘗試。 (無法找到dlopen函數,因此無法加載共享庫。)

回答

0

「cellForRowAtIndexPath」委託存在問題。 根據您的邏輯tableview dequeueReusableCells與索引號,這導致最後一行的丟失,而不是選定的行刪除。可能是您可以更改cellForRowAtIndexPath中的邏輯,也可以使用適當的數據結構算法來管理表視圖。

相關問題