2012-01-11 18 views
2

我想實現UITableView我想在每個UITableViewCell中有3個按鈕,我想爲每個按鈕分配Tittles動態地,如果我有'title_array '中的所有標題,那麼我該如何獲得數組的索引爲每個UITableViewCell中的每個按鈕賦予標題?如何在同一個UITableViewCell中使用多個按鈕來實現UITableView?

這裏是我的代碼, 這是不工作的,

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

     //[email protected]"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; 


} 
+0

去定製UITableViewCell。示例應用程序由Apple提供[此處](http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007318) – Ilanchezhian 2012-01-11 11:55:30

+0

構建自定義單元格視圖或者在你的cellForRowAtIndexPath中以編程方式添加三個按鈕。 – Hiren 2012-01-11 11:57:32

+0

@Adhira:你給出的示例代碼沒有幫助,因爲它使用每個單元的單個按鈕並且易於實現。 – 2012-01-11 12:05:02

回答

0

你應該使用titleArrayNSDictionary對象,在那裏你可以根據單元格中的位置,你的按鈕提供標題(強制例如,你可以使用任何東西來標識你的按鈕)。

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

希望它可以幫助..

相關問題