【问题标题】:tableview update with new row keeping previous rows at first表视图更新,新行首先保留以前的行
【发布时间】:2014-03-19 18:35:48
【问题描述】:

搜索了很多没有找到合适的解决方案来更新一个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];

和tableview数据源委托方法

- (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

但不幸的是没有找到任何关于如何使用它的好帮助。 任何帮助将不胜感激......。

【问题讨论】:

  • 首先你没有正确地对单元格进行双端队列。在 deque 单元格之后,您再次在 getCellContent 视图中初始化单元格,这是错误的。
  • @faiziii 请你告诉我正确的方法。

标签: ios iphone objective-c ipad uitableview


【解决方案1】:

首先更新您的数据源,以便 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

【讨论】:

  • 你能告诉我根据我的代码我应该把这段代码放在哪里……我是 iOS 的几天开发人员,所以我不知道你到底在建议我什么……
  • @Zaibi 看看我的更新答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 2021-02-16
相关资源
最近更新 更多