2011-04-28 54 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):我用它和它的作品很好,但我不知道知道你需要什麼