iOS uitableview reloadData 不太工作
我有一个 UITableView,数据是从外部存储的数据库中提取的。显然,获取该数据需要一些时间,当应用程序启动时,表用于其数据的数组中没有数据。
当从外部源加载数据时,我调用 [self.tableview reloadData];
但有一个小问题。第一个单元格中没有任何文本,直到通过选择它或将其滚动到屏幕之外来重新绘制它。我尝试添加对 [self.tableView setNeedsLayout];
和 [self.tableView setNeedsDisplay]
的调用,但这没有明显效果。
该数组在重新加载表时包含正确的数据,因此这不是竞争条件(我相信!)。
关于可能导致此问题的任何其他想法?
@implementation EXPMasterViewController
@synthesize detailViewController = _detailViewController;
@synthesize partiesModel = _partiesModel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Master", @"Master");
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
self.partiesModel = [[Parties alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadData)
name:@"ReloadData"
object:nil];
}
return self;
}
-(void)reloadData{
[self.tableView reloadData];
[self.tableView setNeedsLayout];
[self.tableView setNeedsDisplay];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0 animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.partiesModel.partiesArray) return [self.partiesModel.partiesArray count];
else return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [[self.partiesModel.partiesArray objectAtIndex:indexPath.row] name];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[[EXPDetailViewController alloc] initWithNibName:@"EXPDetailViewController" bundle:nil] autorelease];
}
self.detailViewController.detailItem = [self.partiesModel.partiesArray objectAtIndex: indexPath.row];
}
I have a UITableView and the data is pulled from a database stored externally. Obviously it takes some time to fetch that data, do when the app is launched there is no data in the array which the table uses for it's data.
When the data is loaded from the external source I call [self.tableview reloadData];
but there is a slight problem. The first cell doesn't have any text in it until after it is redrawn, either by selecting it or by scrolling it off the screen. I've tried adding a call to [self.tableView setNeedsLayout];
and [self.tableView setNeedsDisplay]
but this has no apparent effect.
The array contains the correct data at the time of reloading the table so it's not a race condition thing (I believe!).
Any other ideas about what could be causing this?
@implementation EXPMasterViewController
@synthesize detailViewController = _detailViewController;
@synthesize partiesModel = _partiesModel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Master", @"Master");
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
self.partiesModel = [[Parties alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadData)
name:@"ReloadData"
object:nil];
}
return self;
}
-(void)reloadData{
[self.tableView reloadData];
[self.tableView setNeedsLayout];
[self.tableView setNeedsDisplay];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0 animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.partiesModel.partiesArray) return [self.partiesModel.partiesArray count];
else return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [[self.partiesModel.partiesArray objectAtIndex:indexPath.row] name];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[[EXPDetailViewController alloc] initWithNibName:@"EXPDetailViewController" bundle:nil] autorelease];
}
self.detailViewController.detailItem = [self.partiesModel.partiesArray objectAtIndex: indexPath.row];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您已经在 IB 中完成连接,请取出以下行
Take out the following line if u have already done a connection in the IB
啊修复了它,这与最初选择一行有关。删除
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0animated:NOscrollPosition:UITableViewScrollPositionMiddle];
清除所有内容Ah fixed it, it was something to do with initially selecting a row. Removing
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0 animated:NO scrollPosition:UITableViewScrollPositionMiddle];
cleared it all up