【问题标题】:iOS uitableview reloadData not quite workingiOS uitableview reloadData 不太好用
【发布时间】:2012-01-16 00:31:34
【问题描述】:

我有一个 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];
}

【问题讨论】:

    标签: ios cocoa-touch uitableview reloaddata


    【解决方案1】:

    如果您已经在 IB 中建立了连接,请取出以下行

    messageTableView =[[UITableView alloc] init];
    

    【讨论】:

      【解决方案2】:

      啊修复它,这与最初选择一行有关。删除 [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0 animated:NO scrollPosition:UITableViewScrollPositionMiddle]; 清除一切

      【讨论】:

        猜你喜欢
        • 2012-04-02
        • 1970-01-01
        • 2017-12-14
        • 2017-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-04
        • 2012-09-29
        相关资源
        最近更新 更多