I am using Xcode 4.2 on SnowLeopard, and my project is using storyboards. I am trying to implement a UITableView
with 2 different custom cell types, sessi
if you look at the if else section it shows that the first row is an "sessionCell" and all other rows are "infoCells" what i think you want to do is make the first row an sessionCell, the second row an infoCell and all of the other rows peopleCells
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.people count] + 2; // Added two for sessionCell & infoCell
}
//inside cellForRowAtIndexPath
if(indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
} else if (indexPath.row == 1 {
cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"personCell"];
Person *person = [self.people objectAtIndex:index.path.row - 2];
}
...
return cell;
Better yet, I would try to make two different sell sections, one for info and one for people
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return section == 0 ? 2 : self.people.count
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell = nil;
if (indexPath.section == 0) {
if(indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
// configure session cell
} else if (indexPath.row == 1 {
cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
// configure info cell
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
Person *person = [self.people objectAtIndexPath:indexPath.row];
// configure person cell
}
return cell;
}
If I understand your question correctly, the first infoCell
(second UITableView
row) should display the first person object's data, right?
Then it seems you want:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *sessionCellID = @"sessionID";
static NSString *infoCellID = @"infoID";
if( indexPath.row == 0 ) {
SessionCellClass *cell = nil;
cell = (SessionCellClass *)[tableView dequeueReusableCellWithIdentifier:sessionCellID];
if( !cell ) {
// do something to create a new instance of cell
// either alloc/initWithStyle or load via UINib
}
// populate the cell with session model
return cell;
}
else {
InfoCellClass *cell = nil;
cell = (InfoCellClass *)[tableView dequeueReusableCellWithIdentifier:infoCellID];
if( !cell ) {
// do something to create a new instance of info cell
// either alloc/initWithStyle or load via UINib
// ...
// get the model object:
myObject *person = [[self people] objectAtIndex:indexPath.row - 1];
// populate the cell with that model object
// ...
return cell;
}
}
and you need to return [[self people] count] + 1
for the row count:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self people] count] + 1;
}
so that the n'th row shows the (n-1)th data.