数据源变更时,table中更新对应cell的显示
2015-04-10 14:27
183 查看
要点是用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:index
inSection: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:index
inSection: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:index
inSection: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
{
static
NSString* CellIdent =@"cell";
UITableViewCell* cell = [tableView
dequeueReusableCellWithIdentifier: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;
}
#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:index
inSection: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:index
inSection: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:index
inSection: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
{
static
NSString* CellIdent =@"cell";
UITableViewCell* cell = [tableView
dequeueReusableCellWithIdentifier: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;
}
相关文章推荐
- UIMenuItem在tableview中对应cell显示
- 浅析JTable与TableModel、TableCellRenderer、TableCellEditor接口——使用JComboBox显示单元格的值
- 浅析JTable与TableModel、TableCellRenderer、TableCellEditor接口——使用JComboBox显示单元格的值
- [IOS Tableview] cell自定义view显示错误问题
- iOS开发- TableView不显示没内容的Cell
- 点击tableview上的btn 获得它所属于的cell并获得数据源的行数
- 如何解决关于TableView里面cell随机显示的问题
- IOS中TableView的cell显示问题
- 使用storyboard显示UITableView时,如果不修改系统默认生成的tableView:cellForRowAtIndexPath:方法中的代码,则必须为UITableViewCell注册(填写)重用标识符:identifier.必须要代码方法中的标识符一致.
- TableView不显示没内容的Cell
- div中图片(大图)居中显示:table-cell;max-width关于i8 bug
- 浅析JTable与TableModel、TableCellRenderer、TableCellEditor接口——使用JComboBox显示单元格的值
- 如何解决关于TableView里面cell随机显示的问题
- plain类型的tableView如何不显示多余的空cell
- 当tableview中每个cell对应个controller。简洁的写法如:
- 浅析JTable与TableModel、TableCellRenderer、TableCellEditor接口——使用JComboBox显示单元格的值
- 如何解决关于TableView里面cell随机显示的问题
- 浅析JTable与TableModel、TableCellRenderer、TableCellEditor接口——使用JComboBox显示单元格的值
- ios tableview不能显示最下面一个cell
- winform datagridview 不显示默认第一列 不显示未绑定列 数据源发生改变时自动更新 (转)