是否有可能像使用按鈕一樣使用tableView單元格?在我的應用程序中,我希望有一個有4行的表視圖,每行都會有標籤。我想要的是,當我觸摸一行時,table tableview單元格應該像按鈕一樣反應(執行一個動作並且背景突出顯示)?那可能嗎?我怎樣才能做到這一點?謝謝。iphone開發:使用tableview單元格作爲按鈕
0
A
回答
4
非常簡單的委託方法將被稱爲didSelectRowAtIndexPath那裏你可以得到哪一行被挖掘在哪個部分,你可以執行進一步的操作。
例如
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// deselect
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == 0){
LoginFormViewController *vController = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
vController = [[LoginFormViewController alloc] initWithNibName:@"LoginFormViewController_ipad" bundle:nil];
}else {
vController = [[LoginFormViewController alloc] initWithNibName:@"LoginFormViewController" bundle:nil];
}
[self.navigationController pushViewController:vController animated:YES];
[vController release];
}
}
3
您可以實現在此的tableView「按鈕」功能:在您的UITableView委託didSelectRowAtIndexPath方法方法)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// do something when pressed on an UITableViewCell
}
2
的CEL將突出,和你的表視圖的委託無論如何都會收到tableView:didSelectRowAtIndexPath:
消息。你不需要任何額外的東西來實現你所描述的。
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleGray;
}
if(tableView.tag==1)
{
cell.userInteractionEnabled = NO;
cell.backgroundColor =[UIColor blackColor];
UIButton *Btn11=[UIButton buttonWithType:UIButtonTypeCustom];
[Btn11 addTarget:self action:@selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside];
[Btn11 setBackgroundImage:[UIImage imageNamed:@"small_img4.png"] forState:UIControlStateNormal];
Btn11.tag=indexPath.row;
Btn11.frame=CGRectMake(0, 6.5, 156, 74.5);
UILabel *Main_tblImg_Label=[[UILabel alloc]initWithFrame:CGRectMake(0, 80,156, 31)];
[Main_tblImg_Label setBackgroundColor:[UIColor blackColor]];
//[Main_tblImg_Label setText:@"First Drive 2012 Mercedes Benz M class"];
//Main_tblImg_Label.textAlignment = UITextAlignmentCenter;
[Main_tblImg_Label setTextColor:[UIColor whiteColor]];
[Main_tblImg_Label setFont:[UIFont fontWithName:@"Helvetica" size:9]];
[Main_tblImg_Label setNumberOfLines:2];
[cell.contentView addSubview:Main_tblImg_Label];
[cell.contentView addSubview:Btn11];
UILabel *other_Lbl=[[UILabel alloc]initWithFrame:CGRectMake(14, 80, 142, 29)];
[other_Lbl setText:@"First Drive 2012 \nMercedes Benz M class"];
[other_Lbl setBackgroundColor:[UIColor blackColor]];
[other_Lbl setTextColor:[UIColor whiteColor]];
[other_Lbl setFont:[UIFont fontWithName:@"Helvetica" size:9]];
[other_Lbl setNumberOfLines:2];
[cell.contentView addSubview:other_Lbl];
[cell.contentView addSubview:Btn11];
//[cell.contentView addSubview:Main_tblImg_Label];
// [cell.contentView addSubview:Btn11];
//
UIButton *Btn22=[UIButton buttonWithType:UIButtonTypeCustom];
[Btn22 addTarget:self action:@selector(ratingAction2:) forControlEvents:UIControlEventTouchUpInside];
[Btn22 setBackgroundImage:[UIImage imageNamed:@"Screen2.png"] forState:UIControlStateNormal];
Btn22.tag=indexPath.row;
Btn22.backgroundColor=[UIColor clearColor];
Btn22.frame=CGRectMake(165, 6.5, 156, 74.5);
UILabel *main_tblImg_Lbl2=[[UILabel alloc]initWithFrame:CGRectMake(165, 80, 156, 31)];
[main_tblImg_Lbl2 setBackgroundColor:[UIColor blackColor]];
//[main_tblImg_Lbl2 setText:@"Audi planning Mexican Assembly plant?"];
[main_tblImg_Lbl2 setTextColor:[UIColor whiteColor]];
//[main_tblImg_Lbl2 setFont:[UIFont fontWithName:@"times" size:1]];
[main_tblImg_Lbl2 setFont:[UIFont fontWithName:@"Helvetica" size:10]];
[main_tblImg_Lbl2 setNumberOfLines:2];
[cell.contentView addSubview:main_tblImg_Lbl2];
UILabel *other1_Lbl=[[UILabel alloc]initWithFrame:CGRectMake(176, 80, 136, 31)];
[other1_Lbl setText:@"Audi planning Mexican Assembly plant?"];
[other1_Lbl setBackgroundColor:[UIColor blackColor]];
[other1_Lbl setTextColor:[UIColor whiteColor]];
[other1_Lbl setFont:[UIFont fontWithName:@"Helvetica" size:10]];
[other1_Lbl setNumberOfLines:2];
[cell.contentView addSubview:other1_Lbl];
[cell.contentView addSubview:Btn11];
[cell.contentView addSubview:Btn22];
}
return cell
}
相關問題
- 1. tableview中的單選按鈕單元格
- 2. 使用UIViewController作爲TableView單元格
- 3. Swift 3.0 TableView單元格按鈕
- 4. 在tableview單元格中標記按鈕
- 5. Tableview單元格按鈕選擇錯誤
- 6. Tableview自定義單元格按鈕
- 7. 作爲按鈕的表格單元格
- 8. 在Android上的TableView單元格中使用透明按鈕
- 9. 表視圖單元格作爲按鈕
- 10. 使用[tableView dequeueReusableCellWithIdentifier:forIndexPath:]將單元格中的按鈕添加到按鈕中。
- 11. tableView原型單元格帶外部動作按鈕視圖
- 12. 當按下TableView單元格中的按鈕時,我怎麼知道按下了哪一行單元格按鈕?
- 13. 在tableview單元格中使'Read More'按鈕
- 14. iPhone SDK:使用專用按鈕打開單元格,而不是通過單元格點擊
- 15. 哪個單元格按鈕在swift中按下了tableView 3
- 16. 在按下按鈕時在TableView單元格上添加圖片
- 17. 使用手機上的空格按鈕作爲按鈕單擊
- 18. 如何禁用Tableview單元格的插入按鈕
- 19. 選擇iPhone上的tableView單元格
- 20. iPhone中的tableview單元格選擇
- 21. IOS處理事件單擊按鈕在tableView單元格
- 22. JavaFX TableView自定義單元格渲染分割菜單按鈕
- 23. iPhone問題:如何將一個按鈕添加到tableview單元格?
- 24. 當選擇tableView單元格時更改單元格上的按鈕圖像
- 25. 將目標添加到tableview單元格內的collectionview單元格的按鈕
- 26. 我可以安全使用dequed tableView單元格作爲原型
- 27. Handsontable着色單元格使用按鈕
- 28. iPhone開發 - 的TableView物業
- 29. TableView單元格擴展不會發生在單個單元格
- 30. iPhone Tableview在水平滾動中使用單元格不垂直
有關更改高亮顏色是什麼? – death7eater
你有灰色或藍色的選項,除此之外,你可以製作一個圖像,並使用UIImageView作爲單元格的'''''''''''選擇背景視圖' – jrturton
哦......非常感謝:) – death7eater