我的目标是在分组tableview中插入一个完全填充它的按钮,如Facebook或iPad上的skype登录按钮。要做到这一点,我用这个代码:使用UIButton填充UITableCellView
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Login";
UITableViewCell* cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 1 && indexPath.row == 0){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:cell.frame];
[button setTitle:@"Do Stuff" forState:UIControlStateNormal];
[cell addSubview:button];
}
return cell;
}
但结果是这样的:
是不是我想要的按钮比细胞更广泛,它的位置不正确。 我解决了做各种测试,以找到按钮框架的正确值,但我认为这不是最好和最优雅的解决方案。有没有人有比我更好的解决方案?
而是与此代码:
if(indexPath.section == 1 && indexPath.row == 0){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:cell.bounds];//note here bounds
[button setTitle:@"Do Stuff" forState:UIControlStateNormal];
[cell.contentView addSubview:button];
}
return cell;
结果是:
按钮左上角的顶点与单元格的顶点一致,但仍大于单元格。在我的问题中,我改变了一个更有意义的形象。 – LuckyStarr 2011-12-22 10:54:56
请以图片形式发布你需要什么样的最终结果 – 2011-12-22 10:57:19
更新只需粘贴此代码 – 2011-12-22 11:22:16