繁体   English   中英

索引路径行的TableView单元格每次都不被调用

[英]TableView cell for row at index path is not beeing called every time

我创建了一个类来生成票证,当票证生成时,我想在表格视图中逐一显示它。 我已经为该类创建了一个协议,并且在准备好票证之后,我向其委托发送了一条消息,即tableView以重新加载表视图。 tableView上调用reload方法时- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section每次生成新票证时都会调用- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section但是- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不会在每次生成票证时都被调用,但是一旦所有票证都被生成,就会被调用

下面是代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.ticketGenerator == nil) {
        return 0;
    }
    else{
        return self.ticketGenerator.ticketNumbers.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    HSEticketView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[HSEticketView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell Tickets:[self.ticketGenerator.ticketNumbers objectAtIndex:indexPath.row]];
    }

    // Configure the cell...

    return cell;
}

//to increase the height of the cell

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}

-(void)background
{
    [self.ticketGenerator GenerateNoOfTickets:[self.enteredNo.text intValue]:self];
}


- (IBAction)done:(id)sender {
    [self.enteredNo resignFirstResponder];
    self.enteredNo.hidden = YES;
    self.label.hidden = YES;
    self.button.hidden = YES;
    self.tableView.hidden = NO;

    [self performSelectorInBackground:@selector(background) withObject:nil];
        NSLog(@"dgf");

}

#pragma ticketGenrate delgate methods

-(void)generationOfTicketCompleated
{
    [self.tableView reloadData];


}

对UI的所有更改都必须在主线程上完成 如果执行

[self.ticketGenerator GenerateNoOfTickets:...]

在后台线程和(我假设)该函数调用

[tableView insertRowsAtIndexPaths:...]

那将不起作用,因为必须在主线程上insertRowsAtIndexPaths

如果要从后台线程更新表视图,则可以例如执行

dispatch_async(dispatch_get_main_queue(), ^{
    add item to data source array ...;
    [tableView insertRowsAtIndexPaths:...];
});

检查您的退货self.ticketGenerator.ticketNumbers.count; 为0,则不调用cellForRowAtIndexPath或像[reload YourTableViewName]一样在ViewWillAppear中重新加载表格视图。

谢谢你马丁

问题是我正在另一个线程上执行[tableView reloadData],因为uielements需要在主线程上运行

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

不被称为我得到的解决方案是

-(void)generationOfTicketCompleated
{
    dispatch_async(dispatch_get_main_queue(), ^{

        [self.tableView reloadData];
    });

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM