数据源变更时,table中更新对应cell的显示
来源:互联网 发布:j2ee怎么更新到java 编辑:程序博客网 时间:2024/06/05 18:59
要点是用cellForRowAtIndexPath取cell时,如果取得的是nil,则不用更新这个cell。因为在cell的重用机制下,这个cell在显示时会用新的数据去显示的,所以如果取得nil,则不用处理。
#import "ViewController.h"
@interface Person :NSObject
@property(strong)NSString* name;
@end
@implementation Person
@end
@interface ViewController ()
{
NSArray* _dataSource;
}
@property (weak, nonatomic) IBOutletUITableView *_table;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
NSMutableArray* persons = [NSMutableArrayarray];
for(int i=0;i<20;i++){
Person* person = [Personnew];
person.name = [NSStringstringWithFormat:@"person%d",i];
[personsaddObject:person];
}
_dataSource = [NSArrayarrayWithArray:persons];
[self._tablereloadData];
}
- (IBAction)changeAction:(id)sender {
//修改可见的cell
{
NSInteger index =1;
Person* person = (Person*)_dataSource[index];
person.name = [NSStringstringWithFormat:@"change%ld",(long)index];
NSIndexPath* myIndexPath =[NSIndexPathindexPathForRow:indexinSection:0];
UITableViewCell* cell = [self._tablecellForRowAtIndexPath:myIndexPath];
//没有取到,则说明这个cell没有显示,在滚动显示,会用重用机制显示更改后的内容。
if(cell){
cell.textLabel.text = person.name;
}
}
//修改不可见的cell
{
NSInteger index =9;
Person* person = (Person*)_dataSource[index];
person.name = [NSStringstringWithFormat:@"change%ld",(long)index];
NSIndexPath* myIndexPath =[NSIndexPathindexPathForRow:indexinSection:0];
UITableViewCell* cell = [self._tablecellForRowAtIndexPath:myIndexPath];
//没有取到,则说明这个cell没有显示,在滚动显示,会用重用机制显示更改后的内容。
if(cell){
cell.textLabel.text = person.name;
}
}
//修改不可见的cell
{
NSInteger index =12;
Person* person = (Person*)_dataSource[index];
person.name = [NSStringstringWithFormat:@"change%ld",(long)index];
NSIndexPath* myIndexPath =[NSIndexPathindexPathForRow:indexinSection:0];
UITableViewCell* cell = [self._tablecellForRowAtIndexPath:myIndexPath];
//没有取到,则说明这个cell没有显示,在滚动显示,会用重用机制显示更改后的内容。
if(cell){
cell.textLabel.text = person.name;
}
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return_dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString* CellIdent =@"cell";
UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:CellIdent];
if (cell ==nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdent];
}
Person* person = (Person*)_dataSource[indexPath.row];
cell.textLabel.text = person.name;
cell.accessoryType =UITableViewCellAccessoryNone;
return cell;
}
- 数据源变更时,table中更新对应cell的显示
- UIMenuItem在tableview中对应cell显示
- 为什么我在table view cell中添加的按钮不显示
- 寻找table中得cell 以及对cell的动画
- 神奇的table-cell
- IOS中TableView的cell显示问题
- cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题
- cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题
- cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题
- cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题
- cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题
- IOS Table中Cell的重用reuse机制分析
- IOS Table中Cell的重用reuse机制分析
- IOS Table中Cell的重用reuse机制分析
- IOS Table中Cell的重用reuse机制分析
- IOS Table中Cell的重用reuse机制分析
- IOS Table中Cell的重用reuse机制分析
- IOS Table中Cell的重用reuse机制分析
- 火狐自动清理缓存
- qt做的代码计算器
- eclipse实用快捷键大全
- Android-LineaLayout之android:layout_weight
- iOS 百度地图获取照片的属性信息
- 数据源变更时,table中更新对应cell的显示
- 移动端页面坑
- POJ 2826 An Easy Problem?!(计算几何)
- Java虚拟机工作原理详解
- 处女博客--写博客的之前的一点感悟
- [Swift]结构体
- Its Easy to Launch a Wireless Deauthentication Attack
- Android自定义类似ProgressDialog效果的Dialog
- 有关view加载涉及到的几个方法