开发者

(iphone) how to update only one row in tableview

I hava a tableview,and I will updata the data开发者_如何学Python in the tableview.it receive datas every one second,when the data is received,it will replace the old data in one row. I do like this: when the data is received,i call

[m_tableView reloadData];

so,it will call

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

so i will change the specific row data in this method,but how can i set the row index?


Have you tried

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

Swift

func reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation)

This will allow you to reload a single row.

The reloadData method reloads the whole table


As far as I understand, this method will be called for each UITableViewCell that is visible when you call ReloadData.

If you have updated the underlying datasource, the cell containing your data will be updated.


Just assign the row to an integer and you can access it from that method.

NSInteger row = [indexPath row];


If you are trying to reload a specific row

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

From apple documentation

Parameters indexPaths An array of NSIndexPath objects identifying the rows to reload. animation A constant that indicates how the reloading is to be animated, for example, fade out or slide out from the bottom. See “Table Cell Insertion and Deletion Animation” for descriptions of these constants. The animation constant affects the direction in which both the old and the new rows slide. For example, if the animation constant is UITableViewRowAnimationRight, the old rows slide out to the right and the new cells slide in from the right.


In this method ,

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

u can write this code to update only a particular row whenever u call reloadData method. Like this,

if(indexPath.row==2){
     cell.textLabel.text=@"New updated Value";
 }

This will updated only third row, every time.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜