2016-07-12 113 views
0

我有一個數量的基本tableview在那加按鈕,將在tableview中添加新項目。
雖然我嘗試添加或刪除tableview中的部分並重新加載該tableview它會多次閃爍舊記錄,然後添加或刪除該部分。
這裏是代碼。UITableview插入/刪除

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SymbolTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if ([selectIndexPath containsObject:[NSNumber numberWithInt:(int)indexPath.section]]) 
    { 
     [selectSymbol removeObject:cell.lblsymbol.text]; 
     [selectIndexPath removeObject:[NSNumber numberWithInt:(int)indexPath.section]]; 
    } 
    else 
    { 
     NSString *strr = cell.lblsymbol.text; 
     [selectSymbol addObject:strr]; 
     [selectIndexPath addObject:[NSNumber numberWithInt:(int)indexPath.section]]; 
    } 
    [tblDetail reloadData]; 
} 


是什麼問題?

+0

**永不**從視圖(表格視圖單元格)獲取像'cell.lblsymbol.text'這樣的信息,從模型(數據源數組)中獲取** always ** – vadian

+0

怎麼樣?我沒有主意。 –

+0

您應該使用'reloadRowsAtIndexPaths'或'reloadSections'和'insert/deleteRowsAtIndexPaths'方法。這樣它就不會再重新載入整個表格,只需重新載入表格的所需部分。 –

回答

0

您可以重新加載您刪除/添加的單元格 您不會重新加載所有表格。 嘗試下面的代碼,請

[self.tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationFade]; 

[self.tableView insertRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationFade]; 
+0

中標籤的'text'屬性的信息,我在tableview中有部分。它工作嗎? –

+0

它應該工作 – MirMiratom

+0

它崩潰的應用程序 –

0

試試下面的代碼它可能會幫助你

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    SymbolTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if ([selectIndexPath containsObject:[NSNumber numberWithInt:(int)indexPath.section]]) 
    { 
    [tblDetail beginUpdates]; 
    [selectSymbol removeObject:cell.lblsymbol.text]; 
    [selectIndexPath removeObject:[NSNumber numberWithInt:(int)indexPath.section]]; 
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    [tableView endUpdates]; 
    } 
    else 
    { 
    NSString *strr = cell.lblsymbol.text; 
    [tblDetail beginUpdates]; 
    [selectSymbol addObject:strr]; 
    [selectIndexPath addObject:[NSNumber numberWithInt:(int)indexPath.section]]; 
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    [tableView endUpdates]; 
    } 
} 
+0

它在刪除行時崩潰。 –

+0

我正在編輯我的post.can你列出錯誤? –

+0

- [UITableView _endCellAnimationsWithContext:], 2016-07-12 17:08:56。750 DigiTicks [3873:109738] ***由於未捕獲異常'NSInternalInconsistencyException',原因:'無效更新:第0節中的行數無效,終止應用程序。更新(1)後,現有節中包含的行數必須等於在更新(1)之前包含在該部分中的行數,加上或減去從該部分插入或刪除的行數(插入0,刪除1),加上或減去移入或移出的行數(移入0,移出0)。' –

0
for inserting row in tableview 

    - (void)insertNewObject:(id)sender 
{ 
    if (!arrayForTableContent) { 
     arrayForTableContent = [[NSMutableArray alloc] init]; 
    } 
    NSInteger count = [arrayForTableContent count]; 
    NSString *strTobeAdded = [NSString stringWithFormat:@"%d",count+1]; 
    [arrayForTableContent insertObject:strTobeAdded atIndex:count]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count inSection:0]; 
    [self.tableViewForScreen insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 
+0

在你想添加的地方調用這個方法 – Birendra

0

我不寫所有的代碼,也沒有測試它在調試器中,但我會給你一個如何正確imp的建議說這個。假設您有一個數據源,如下所示: NSMutableArray * data = @ [@「First」,@「Second」,@「Third」];

  • 數據源:在numberOfRowsInSection中,返回data.count;在cellForRowAtIndexPath中,您將使用數組中的相應索引配置單元格;
  • 委託:在didSelectRowAtIndexPath中,從數據數組中刪除對象,說你需要這樣的東西:[data removeObjectAtIndex(indexPath.row)];然後在表視圖上調用reloadData。表視圖將順利返回現在只有兩個項目。