Ranganatha G V
Ranganatha G V

Reputation: 415

How To implement UITableView with multiple buttons in same UITableViewCell?

I want to implement UITableView Where I want to have 3 buttons in each UITableViewCell, I want to assign Tittles to each buttons Dynamically, if am having all titles in 'title_array ' say then how can I get the index to an array for giving tittles to each buttons in each UITableViewCell ?

here is my code , which is not working,

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   // printf("\ncellForRowAtIndexPath\n");
    //static NSString *CellIdentifier = @"Cell";

    NSString *CellIdentifier = [NSString stringWithFormat:@"identifier_%d", indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    //if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        //cell.textLabel.text=@"Golbal VC";
    int x=50;

    if (indexPath.row == 0) {
        sample = 0;
    }else{
        if (sample == [titles_array count]-1);
        else sample += 2;
    }


    for(int i =0; i <2 ; i++){


        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(x,50,300, 300)];
        [btn setBackgroundImage:[UIImage imageNamed:@"Canada.gif"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; 
        [cell.contentView addSubview:btn];


        UILabel *tit_lbl=[[UILabel alloc]initWithFrame:CGRectMake(10,10,280,45)];
        [tit_lbl setText:[titles_array objectAtIndex:sample]];
        [btn addSubview:tit_lbl];
        [tit_lbl release];

        UILabel *src_lbl=[[UILabel alloc]initWithFrame:CGRectMake(10,65,280,25)];
        [src_lbl setText:[source_array objectAtIndex:sample]];
        [btn addSubview:src_lbl];
        [src_lbl release];

        x=x+350;
        /*if(index == [titles_array count]-1)
            break;
        index++;        
        */
    }

  return cell;


}

Upvotes: 2

Views: 3402

Answers (2)

Mehul Mistri
Mehul Mistri

Reputation: 15147

You should have to try using this link and create custom cell accordingly and put three buttons in your UITableViewCell XIB file... This is the simplest method for doing this... and assign tag to each button in XIB...

  1. http://blog.webscale.co.in/?p=284
  2. http://www.e-string.com/content/custom-uitableviewcells-interface-builder
  3. http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/

Upvotes: 1

Shrey
Shrey

Reputation: 1977

you should use titleArray with NSDictionary objects, where you can provide title to your buttons according to the position in the cell( jus for example, you can use anything to identify your buttons).

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];         
    }

    NSDictionary *dict = (NSDictionary*)[titleArray objectAtIndex:indexPath.row]; 

    UILabel *tit_lbl=[[UILabel alloc]initWithFrame:CGRectMake(10,10,280,45)];
    [tit_lbl setText:[dict valueForKey:@"titleLabel"]];
    [btn addSubview:tit_lbl];
    [tit_lbl release];  

    UILabel *src_lbl=[[UILabel alloc]initWithFrame:CGRectMake(10,65,280,25)];
    [src_lbl setText:[dict valueForKey:@"sourceLabel"]];
    [btn addSubview:src_lbl];
    [src_lbl release];   

    return cell;
}

Hope it helps..

Upvotes: 0

Related Questions