I’m creating a UITableView that uses custom UITableViewCell classes. The UITableViewCell uses it’s own methods to lay itself out depending on the indexPath and selected index.
Code is as follows:
AgendaView.h
TableViewDelegate Methods
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
EventInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
if(cell == nil){
cell = [[EventInfoCell alloc]initWithStyle:0 reuseIdentifier:@"infoCell"];
}
cell.frame = CGRectMake(0, 0, 320, [self tableView:tableView heightForRowAtIndexPath:indexPath]);
cell.selectedBackgroundView = nil;
cell.delegate = self;
TimerEvent *event = [_eventsArray objectAtIndex:indexPath.row];
EventInfo *info = [event valueForKey:@"eventInfo"];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"h:mma"];
cell.timeLabel.text = [NSString stringWithFormat:@"%@",[df stringFromDate:event.date]];
cell.titleField.text = [info title];
cell.bgImageView.frame = cell.frame;
cell.countdownLabel.text = [self getCountdown:event];
if (_selectedIndex == indexPath.row){
[cell layoutSelected:YES editing:[tableView isEditing]];
if ([info location])
cell.locationField.text =[NSString stringWithFormat:@"@%@",[info location]];
else
cell.locationField.text = @"Location";
if ([info notes])
cell.notesView.text = [info notes];
else
cell.notesView.text = @"Notes";
}else
[cell layoutSelected:NO editing:NO];
return cell;
}
Is this an acceptable way to layout a cell? Or should I create different cell subclasses for the different cell layouts? If it helps, the cell’s expand when touched to show more information, which is why I had the subclass handle it’s own layouts.
Images: http://tinypic.com/r/2vjpi12/6
The cells information is determined by the TimerEvent/Info assigned during tableView:cellForRowAtIndexPath.
I think you should instead create different UITableViewCell subclasses for each type of row. This is a sketch of how I might do it:
Your table view data source (probably your view controller):
EDIT: extended explanation of the above:
The first part of the code (the category) on UITableViewCell extends it to provide a
cellIdentifierclass method and avalueproperty. Now all instances of UITableViewCell support setting/getting the “value” property, including your subclasses.Let’s say you have to types of row, Employees and Companies. You would create
EmployeeCellandCompanyCell: