把一个UIButton放在UITableView里面
问题描述:
我想把一个UIButton
放在UITableViewCell
里面。把一个UIButton放在UITableView里面
UITableView
也具有包含UILabels
的单元格,并且单元格具有渐变背景。我看到的问题是我的UIButton
只是一个填充黑色单元格,没有文字显示,并且改变颜色和背景颜色属性似乎没有任何作用。
下面是我用它来创建按钮的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"InAppViewController, doing this - width= %f", self.wdth);
static NSString *ProductTableIdentifier = @"ProductTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ProductTableIdentifier];
if (cell == nil)
{
if (self.wdth >= 700) {
CGRect cellFrame = CGRectMake(0, 0, (self.wdth - 100), 40);
cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier: ProductTableIdentifier] autorelease];
// Set up some labels...
CGRect seenValueRect = CGRectMake(self.wdth-320, 0, 100, 40);
UIButton *seenValue = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[seenValue setBackgroundColor:[UIColor greenColor]];
seenValue.tag = kSeenValueTag;
seenValue.frame = seenValueRect;
[seenValue setTitle:@"I'm a clone" forState:UIControlStateNormal];
[cell.contentView addSubview:seenValue];
[seenValue release];
// Continue setting up cell...
该引擎收录链接中包含的代码对整个cellForRowAtIndexPath()
功能(有问题的UITableView
)。
答
也许问题是,你释放的UIButton与便利初始化[UIButton buttonWithType:(UIButtonType)]
innitialized。尝试删除[button release]
+1
好眼睛,这可能是它 – 2011-04-28 15:40:53
答
它甚至不能是你必须做的[cell addSubview:seenValue];
代替[cell.contentView addSubview:seenValue];
你也可以尝试做CGRectMake(110, 11, 185, 30)
而不是CGRectMake(self.wdth-320, 0, 100, 40)
:我用它和它的作品很好,但我不知道知道你需要什么
看看iOS UITableView文档中的* Customizing Cells *部分:http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/ TableViewCells/TableViewCells.html – 2011-04-28 15:09:41