2008-12-05 146 views
7

我想我的一個錶行是一個按鈕,佔用我的UITableView的整行。我認爲最好的方法是實例化一個UIButton,並將它作爲UITableViewCell的實例賦予相同的幀大小,並將其作爲子視圖添加到單元格中。我差不多在那裏,但是有很多像素沒有得到完美的感覺。有沒有更好的方法來解決這個問題,或者可以通過修正我的貼裝精度來獲得完美的對齊方式嗎?UIButton覆蓋UITableViewCell

cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell == nil) { 
    cell = [self tableviewCellWithReuseIdentifier:CellIdentifier]; 
} 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setFrame:CGRectMake(0.0f, 5.0f, 320.0f, 44.0f)]; 

[button setTitle:@"Do Stuff" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(doStuff:) forControlEvents:UIControlEventTouchUpInside]; 
[cell addSubview:button]; 
+0

您可以在將來將您的問題留下過時的標記「objectivec」嗎?另外,「可可」並不適合iPhone的問題。謝謝! – 2008-12-05 21:42:22

回答

13

你可以在你的didSelectRowAtIndexPath方法中僞裝它。 讓tableView單元充當按鈕。

[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES]; 
[self doStuff]; 
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES]; 

使用setSelected: animated:假按鈕按下並使其褪色。 可能會爲你做詭計。

+1

我喜歡這個想法。與分組式桌面效果很好。 – 2009-06-19 17:10:04

0

這可能並不重要,但是每次重新使用單元格時,您的代碼可能會出現其他按鈕已添加到單元的問題。

0

我和你有同樣的問題。 Ryan給出了答案:使用單元本身作爲didSelectedRowAtIndexPath:方法的按鈕。

0

你也可以添加一個UIImage到你的UITableViewCell中,使單元看起來像一個按鈕。

0

您也可以使用按鈕創建視圖並將其放置在其上面的部分的頁腳中。這樣你就不必擔心背景中的細胞圖像。我已經完成了這個操作,將兩個半寬按鈕並排放置在分組表格視圖中。

0

FWIW,我把按鈕放在我的應用程序的CGRectMake(9,0,302,45) ,我覺得它看起來很完美。

3

類(UITableViewCell)具有contentView屬性。 所以我把基本按鈕放在UITableViewCell變量的contentView中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ・・・ 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f); 
    [btn setTitle:@"OK" forState:UIControlStateNormal]; 
    [btn setTitle:@"OK" forState:UIControlStateSelected]; 
    [btn addTarget:self action:@selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:btn]; 
    ・・・ 
    return cell; 
} 

對不起,我的英文很差。

0
UIButton *btnView = [[UIButton alloc] init]; 
btnView.frame = CGRectMake(52.0f, 2.0f, 215.0f, 38.0f); 
[btnView setImage:[UIImage imageNamed:@"invite.png"] forState:UIControlStateNormal]; 
[btnView addTarget:self action:@selector(showInviteByMail) forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:btnView]; 
[btnView release]; 
3

如果您希望它完美配合,只需將單元格的邊界作爲CGRect調用即可。這樣做:

button.frame = cell.bounds; 

這應該讓你的按鈕是完全一樣的,你的手機。希望有所幫助!

電線