如何在同一个UITableViewCell中使用多个按钮实现UITableView?

时间:2012-01-11 11:50:51

标签: iphone uitableview

我想实现UITableView我想在每个UITableViewCell中有3个按钮,我想动态地为每个按钮分配Tittles,如果我在'title_array中拥有所有标题 '那么我怎么能得到一个数组的索引来为每个UITableViewCell中的每个按钮提供一些标记?

这是我的代码, 这是行不通的,

- (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;


}

2 个答案:

答案 0 :(得分:1)

您应该尝试使用此链接并相应地创建自定义单元格并在UITableViewCell XIB文件中放置三个按钮...这是执行此操作的最简单方法...并为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/

答案 1 :(得分:0)

您应该将titleArrayNSDictionary个对象一起使用,您可以根据单元格中的位置为按钮提供标题(例如,您可以使用任何内容来识别按钮)。 / p>

- (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;
}

希望有所帮助......