我想实现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;
}
去定制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
构建自定义单元格视图或者在你的cellForRowAtIndexPath中以编程方式添加三个按钮。 – Hiren 2012-01-11 11:57:32
@Adhira:你给出的示例代码没有帮助,因为它使用每个单元的单个按钮并且易于实现。 – 2012-01-11 12:05:02