我想实现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;
}
答案 0 :(得分:1)
您应该尝试使用此链接并相应地创建自定义单元格并在UITableViewCell XIB文件中放置三个按钮...这是执行此操作的最简单方法...并为XIB中的每个按钮分配标记...
答案 1 :(得分:0)
您应该将titleArray
与NSDictionary
个对象一起使用,您可以根据单元格中的位置为按钮提供标题(例如,您可以使用任何内容来识别按钮)。 / 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;
}
希望有所帮助......