2014-03-19 34 views
0

搜索了很多之後的tableview更新未找到妥善的解決辦法,以更新的tableview與新行保持以前行的第一

我想更新我的tableview像一個新的未來紀錄應該將略低於先前更新記錄。

這是我的代碼

if (sqlite3_open(myDatabase, &myConnection) == SQLITE_OK) 
{ 
    sqliteQuery = [NSString stringWithFormat: @"SELECT Description, SalePrice FROM ProductDetails WHERE Barcode = \'%@\'", barcode]; 
    if (sqlite3_prepare_v2(myConnection, [sqliteQuery UTF8String], -1, &sQLStatement, NULL) == SQLITE_OK) 
    { 
     if(sqlite3_step(sQLStatement) == SQLITE_ROW) 
     { 
       temp = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 0)]; 
      } 
      temp1 = [NSString stringWithFormat:@"%d", value]; 
      if ([temp1 isEqualToString:@"0"]) 
      {temp1 = @"1";} 
       temp2 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 1)]     } 
      } 
      [description addObject: temp]; 
      [qty addObject: temp1]; 
      [price addObject:temp2]; 
     } 
     sqlite3_finalize(sQLStatement); 
    } 
    sqlite3_close(myConnection); 
} 
myTable.hidden = NO; 
myTable.delegate = self; 
myTable.dataSource = self; 
[myTable reloadData]; 
[self.view endEditing:YES]; 

和實現代碼如下數據源委託方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
// Configure the cell in each row 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell; 
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell = [self getCellContentView:CellIdentifier]; 

UILabel *lbl11 = (UILabel *)[cell viewWithTag:1]; 
[lbl11 setText:@"Label1"]; 
UILabel *lbl21 = (UILabel *)[cell viewWithTag:2]; 
[lbl21 setText:@"Label2"]; 
UILabel *lbl31 = (UILabel *)[cell viewWithTag:3]; 
[lbl31 setText:@"Label3"]; 

lbl11.text = [description objectAtIndex:indexPath.row]; 
lbl21.text = [qty objectAtIndex:indexPath.row]; 
lbl31.text = [price objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier 
{ 
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; 
cell.backgroundColor=[UIColor clearColor]; 

CGRect lbl11Rect = CGRectMake(20, 5, 450, 30); 
CGRect lbl21Rect = CGRectMake(545, 5, 150, 30); 
CGRect lbl31Rect = CGRectMake(795, 5, 150, 30); 

UILabel *lbl11 = [[UILabel alloc] initWithFrame:lbl11Rect]; 
lbl11.tag=1; 
lbl11.font=[UIFont fontWithName:@"Superclarendon" size:15]; 
lbl11.backgroundColor=[UIColor clearColor]; 
//lbl11.layer.borderWidth = 1.0f; 
[cell.contentView addSubview:lbl11]; 

UILabel *lbl21 = [[UILabel alloc] initWithFrame:lbl21Rect]; 
lbl21.tag=2; 
lbl21.font=[UIFont fontWithName:@"Superclarendon" size:15]; 
lbl21.backgroundColor=[UIColor clearColor]; 
//lbl21.layer.borderWidth = 1.0f; 
[cell.contentView addSubview:lbl21]; 

UILabel *lbl31 = [[UILabel alloc] initWithFrame:lbl31Rect]; 
lbl31.tag=3; 
lbl31.font=[UIFont fontWithName:@"Superclarendon" size:15]; 
lbl31.backgroundColor=[UIColor clearColor]; 
//lbl31.layer.borderWidth = 1.0f; 
[cell.contentView addSubview:lbl31]; 

cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
return cell; 
} 

的第一個值搜索這表明在tableview中的數據...。如何更新的tableview正確 我發現,對於更新

[myTable beginUpdate] 

功能將被使用......而且還有

insertRowsAtIndexPaths: withRowAnimation 

但遺憾的是沒有找到什麼好的幫助,如何使用此。 任何幫助將不勝感激......。

+0

首先,你不是在正確地使細胞出質。在取消單元格之後,您將再次在getCellContent視圖中初始化單元格,這是錯誤的。 – faiziii

+0

@faiziii你會告訴我正確的方式來做到這一點。 –

回答

2

首先更新您的數據源,以便numberOfRowsInSection和cellForRowAtIndexPath將爲您的後插入數據返回正確的值。在插入或刪除行之前,您必須執行此操作。 然後插入你的行:

// First figure out how many sections there are 
    NSInteger lastSectionIndex = [tableView numberOfSections] - 1; 

// Then grab the number of rows in the last section 
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex]; 

// Now just construct the index path 
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]; 

//Adding new data row to last index position: 
[myTable beginUpdates]; 
[myTable insertRowsAtIndexPaths:[NSArray arrayWithObject: pathToLastRow] withRowAnimation:UITableViewRowAnimationNone]; 
[myTable endUpdates]; 

有動畫不同的變體:

UITableViewRowAnimationBottom 
UITableViewRowAnimationFade 
UITableViewRowAnimationMiddle 
UITableViewRowAnimationNone 
UITableViewRowAnimationRight 
UITableViewRowAnimationTop 

從iOS開發者庫:

beginUpdates 
Begin a series of method calls that insert, delete, or select rows and sections of the receiver. 
Discussion 
Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: and indexPathsForVisibleRows) to be animated simultaneously. This group of methods must conclude with an invocation of endUpdates. These method pairs can be nested. If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count might become invalid. You should not call reloadData within the group; if you call this method within the group, you will need to perform any animations yourself. 

使用這種方法,你可以在相關找到實例示例代碼:iPhoneCoreDataRecipes

+0

你會告訴我,我應該在哪裏放置這個代碼根據我的代碼...我有幾天在iOS開發,所以我不知道你到底在暗示我... –

+0

@Zaibi看我的更新回答 –