2011-04-28 53 views
0

我想把一个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)。

http://pastebin.com/bpEyfruE

+0

看看iOS UITableView文档中的* Customizing Cells *部分:http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/ TableViewCells/TableViewCells.html – 2011-04-28 15:09:41

回答

2

也许问题是,你释放的UIButton与便利初始化[UIButton buttonWithType:(UIButtonType)] innitialized。尝试删除[button release]

+1

好眼睛,这可能是它 – 2011-04-28 15:40:53

0

它甚至不能是你必须做的[cell addSubview:seenValue];代替[cell.contentView addSubview:seenValue];

你也可以尝试做CGRectMake(110, 11, 185, 30)而不是CGRectMake(self.wdth-320, 0, 100, 40):我用它和它的作品很好,但我不知道知道你需要什么