gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:18 24 4
gpt4 key购买 nike

我创建了一个类来生成票证,生成票证后我想将其一张一张地显示在表格 View 中。我已经为该类创建了一个协议(protocol),准备好票证后,我会向它的代表 tableView 发送一条消息以重新加载 TableView 。当在 tableView 上调用 reload 方法时 - (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 必须在主线程上调用。

如果你想从后台线程更新 TableView ,你可以这样做

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

关于iphone - 索引路径中行的 TableView 单元格不会每次都被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12725155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com