iOS uitableview reloadData 不太工作

发布于 2024-12-27 04:47:00 字数 2849 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蛮可爱 2025-01-03 04:47:01

如果您已经在 IB 中完成连接,请取出以下行

messageTableView =[[UITableView alloc] init];

Take out the following line if u have already done a connection in the IB

messageTableView =[[UITableView alloc] init];
雄赳赳气昂昂 2025-01-03 04:47:01

啊修复了它,这与最初选择一行有关。删除 [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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文