I have created a class to generate tickets and when the ticket is generated I want to show it in the table view one by one.
I have created a protocol for that class and once the ticket is prepared I send a message to its delegate which is tableView to reload the table view.
when the reload method is called on the tableView - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section is being called every time a new ticket is generated but the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called every time the ticket is generated but once all the tickets are generated it is being called
below is the code
- (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];
}
All changes to the UI must be done on the main thread. If you execute
on a background thread and (as I assume) that functions calls
that will not work, because
insertRowsAtIndexPathsmust be called on the main thread.If you want to update the table view from a background thread, you can for example do