I have populated a NSArray with results of a query from MagicalRecord (a Core Data wrapper). I need to take those results and display them in custom cells in a UITableView from within cellForRowAtIndexPath:, but the index refers to another UITableView that has been selected.
I have two UITableViews; one contains a list of customers, the other contains a list of appointments for each customer. When the user selects a customer, the code finds all of the appointments for that customer. As such, there is no index involved with the second table view).
The first table view has a list of clients; when the user selects one of those clients, the second table view is populated with a list of appointments for that selected client. Thus the index.row refers to the first UITableView and the second has no index it can refer to. I just dump the contents of the apptDataArrray into the cells.
Here is my code:
if(tableView.tag == 2) { // appointment info
static NSString *cellIdentifier = @"apptListCell";
ApptCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[ApptCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// configure the cell...
AppointmentInfo *currentAppointment = [apptDataArray objectAtIndex: 0]; // go through the array <---- TODO
// Set the dateFormatter format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // start date/time
[timeFormatter setDateFormat:@"HH:mm:ss"]; // end time
UILabel *label = (UILabel *)[cell viewWithTag:3]; // display start date/time (NEEDS TO BE LOCALIZED!)<----- TODO????
label.text = [dateFormatter stringFromDate:currentAppointment.aStartTime];
label = (UILabel *)[cell viewWithTag:4]; // display end time
label.text = [timeFormatter stringFromDate:currentAppointment.aEndTime];
label = (UILabel *)[cell viewWithTag:5]; // display service tech name
label.text = currentAppointment.aServiceTech;
return cell;
}
My question is: When at the line below // configure the cell, how do I iterate through the apptDataArray each time cellForRowAtIndexPath: is called, since there is NO index that can be applied to this array?
What?! No, it doesn’t. You have a serious misunderstanding. The full name of that method is
tableView:cellForRowAtIndexPath:. Any table view that needs to display a cell passes itself as the first argument, and it passes the index path for its own single cell* as the second. When the second table view is asking your data source for its cell information — when your testtableView.tag == 2test is true — the passed-in index path refers to that table. How could it possibly work otherwise?All you need to do is
the same as you would for any other array-backed table view.
There’s no magic going on here. You’ve given the table view a pointer to your object and told the table to get information it needs from that object. The table then sends messages, just like anything else, to the data source object. One of those messages is
tableView:cellForRowAtIndexPath:. The fact that another table view also sends that message sometimes doesn’t affect the first. Each table view will interact with your data source only on its own behalf.As a note, this problem — even the description of your app’s structure — indicates strongly that you should probably have a separate data source/delegate object for each of your table views.
*Somewhere in the comments you seemed to imply that you think you need to set up all the cells in the table view during one call of
tableView:cellForRowAtIndexPath:, and that’s not right either. You need to configure and return just the one single cell that corresponds to the index path.