sorry for late answer but u don't need to use the different row if the button count exceeds more than 3, it will be complicated management of tableview, instead why not you use the sing row with different height with all the buttons can fit at a proper place inside the tableview cell.
look at the code below u can try it in the separate project, for the custom cell don't use the default cell that is "UITableViewCell" instead subclass it, there by u can manage the cell contents easily (i suggest)
in the subclassed cell .m file (i took one string for date and the array contains the button title) that is your time
#import <UIKit/UIKit.h>
//you need to use the custom cell
@interface CustomCell : UITableViewCell //subclass the UITableViewCell for complete customization
@property (nonatomic, retain) NSString *titleString;//string for mrng or eve or nyt
@property (nonatomic, retain) NSArray *buttons;//for your buttons
@end
in .m file
#import "CustomCell.h"
@implementation CustomCell
//synthesize them
@synthesize titleString;
@synthesize buttons;
//custom setters method this will handle all the buttons initilizations
- (void)setButtons:(NSArray *)inButtons
{
if(inButtons != buttons)
{
buttons = inButtons;
// [buttons release]; //ARC give's error in this part so comment it
// [inButtons retain];
if(buttons)
{
[self createButtonForThecell];//call your helper to add the buttons to cell
}
}
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//put a label in the cell
UILabel *titleLabel = [[UILabel alloc]init];
titleLabel.tag = 1000;//to get it in the layout subviews
[self addSubview:titleLabel];//add it to cell
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)createButtonForThecell
{
//at this movement u get the buttons array contains the string (buttons) to be placed in the cell
int xValue,Yvalue;
xValue = 63;
Yvalue = 0;
int i = 1;
NSLog(@"buttons->%d",[self.buttons count]);
for(int k = 0 ;k< [self.buttons count];k++)//loop throug the button
{
//you need 3 for each row starts after 90 from x-axis
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(xValue , 2 + Yvalue, 65, 35)];
if(k == (3 * i) || (k % 3) == 2)
{
i = i + 1;
Yvalue += (35 + 5);
xValue = 63;
}
else
{
xValue += (63 + 5);
}
[button addTarget:self action:@selector(whenEventButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[self.buttons objectAtIndex:k] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTag:k];//setting the tag to index it will become use for the acces of which button tapped
//finally add it to cell
[self addSubview:button];
}
}
- (void)whenEventButtonClicked
{
//hear put a delegate to controller for further processing of the event
NSLog(@"button clicked");
}
- (void)layoutSubviews
{
[super layoutSubviews];
//in the this always set the frme
UILabel *label = (UILabel *)[self viewWithTag:1000];
if(label)
{
label.frame = CGRectMake(2, 3, 55, 35);//your label in the left side
label.text = self.titleString;//set the string
label.textColor = [UIColor greenColor];//set the color for your label
}
}
@end
in .m file of controller i took one array which contains dictionaries like below
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
present = NO;
listResultDataAry = [[NSMutableArray alloc]init];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];
[tempDict setObject:@"Mrng" forKey:@"Date"];
[tempDict setObject:[[NSArray alloc]initWithObjects: @"Button1",@"Button2",@"Button3",@"button4",@"button5", nil] forKey:@"time"];
[listResultDataAry addObject:tempDict];
NSMutableDictionary *tempDict1 = [[NSMutableDictionary alloc]init];
[tempDict1 setObject:@"Eveng" forKey:@"Date"];
[tempDict1 setObject:[NSArray arrayWithObjects: @"Button1",@"Button2",@"Button3", nil] forKey:@"time"];
[listResultDataAry addObject:tempDict1];
NSMutableDictionary *tempDict2 = [[NSMutableDictionary alloc]init];
[tempDict2 setObject:@"Nyt" forKey:@"Date"];
[tempDict2 setObject:[[NSArray alloc]initWithObjects: @"Button1",@"Button2",@"Button3",@"Button4",@"button5",@"button6",@"button7",@"button 8", nil] forKey:@"time"];
[listResultDataAry addObject:tempDict2];
}
in table view delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;//return how many sections are there
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listResultDataAry count];//return u hav for mrng,eveng,nyt for the more numbers u can resize the tableview cell
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSMutableDictionary *tempDict = [listResultDataAry objectAtIndex:indexPath.row];
NSLog(@"IndexPath.row = %d",indexPath.row);
cell.titleString = [tempDict objectForKey:@"Date"];//set the date for your label
cell.buttons = [tempDict objectForKey:@"time"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//hear u need to cumpute the height for each cell
NSMutableDictionary *tempDict = [listResultDataAry objectAtIndex:indexPath.row];
NSArray *tempArray = [tempDict objectForKey:@"time"];
CGFloat height = [self computeHeight:tempArray];
return height;
}
- (CGFloat)computeHeight:(NSArray *)array
{
CGFloat height = 40; //0 1 2
int i = 1; //3 4 5
for(int k = 0; k< [array count];k++)
{
if(k == (3 * i))
{
height = height + 40;
i = i + 1;
}
}
return height;
}
hope this helps u ... :)