2011-05-26 60 views
1

我可以用textlabeldetaillabeltext創建tableview如何將UIButton放入uitableview單元格中?

除此之外,是否可以將UIButton添加到單元格中。

的detaillabeltext後,我的意思是,我可以把一個UIButton(如「刪除」按鈕)

目前,我有以下代碼

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
//----------------------------------------------------------------------------------------------------- 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    profileName = [appDelegate.sentItemsList objectAtIndex:indexPath.row]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    NSString *subjectData = [profileName.sent_subject stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    cell.textLabel.text = [NSString stringWithFormat: @"%@ ", subjectData]; 

    cell.textLabel.font = [UIFont boldSystemFontOfSize:13]; 


    NSString *CompanyName = [profileName.sent_content stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    cell.detailTextLabel.text = [NSString stringWithFormat: @"%@ ",CompanyName]; 

    cell.detailTextLabel.font = [UIFont systemFontOfSize:10]; 

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    cell.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:249.0/255.0 blue:230.0/255.0 alpha:2.0]; 

    return cell; 
} 

感謝您的時間和幫助!

+0

可能重複[UIButton的在UITableView的細胞像「刪除事件」(http://stackoverflow.com/questions/1076785/uibutton-in-uitableview- cell-like-delete-event) – 2011-05-26 04:20:12

+0

對於什麼,你想添加按鈕?就像如果你想實現刪除功能一樣,你可以使用附件,否則你將不得不實現所有東西作爲你的表視圖單元格的子視圖。 – rptwsthi 2011-05-26 04:26:55

+0

@rptwsthi:不喜歡刪除功能。我想打電話給我的「添加到收藏夾」按鈕 – user198725878 2011-05-26 04:52:15

回答

6
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button addTarget:self action:@selector(aMethod:) 
forControlEvents:UIControlEventTouchDown]; 
[button setTitle:@"Show View" forState:UIControlStateNormal]; 
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); 
[cell addSubview:button]; 
+0

這是一個正確的解決方案,但它會隱藏所有以前的文本(標籤和子標籤) – rptwsthi 2011-05-26 04:31:33

+0

不,它不會隱藏以前的...正確給予cooridates – Aravindhan 2011-05-26 04:36:57

+1

@rptwsthi :否則你可以看到這個教程是很好的http://www.e-string.com/content/custom-uitableviewcells-interface-builder – Aravindhan 2011-05-26 04:41:07

2

您可以將按鈕或任何其他控件添加到單元格的內容視圖。

UIButton *cellButton = [[UIButton alloc]init]; 
[cellButton addTarget:self action:@selector(uploadData:)forControlEvents:UIControlEventTouchDown]; 
[cellButton setTitle:@"Upload Now" forState:UIControlStateNormal]; 
cellButton.frame = CGRectMake(100, 180, 150, 35); 
[cell.contentview addSubview:cellButton]; 
[cellButton release]; 

任何控件添加單元格的內容視圖的

相關問題