我希望添加單選按鈕到UITableview。單選按鈕的功能應該類似於我選擇單選按鈕的單選按鈕,該單選按鈕應該被選中,而其他單元中的其他單選按鈕應該被取消選中。它應該是種或者,那只有一個單選按鈕可以選擇。單選按鈕裏面的UITableview不工作
-2
A
回答
3
創建一個int
值
int selectstr;
UIButton * button;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 14.0, 215.0, 36.0)];
title.backgroundColor = [UIColor clearColor];
title.textAlignment = NSTextAlignmentLeft;
title.textColor=[UIColor blackColor];
title.tag=indexPath.row;
title.text = @"TEST";
// title.font = [UIFont fontWithName:@"Arial Hebrew" size:16.0f];
title.font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
[cell.contentView addSubview:title];
button = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 46.0f, 30.0f)];
button.tag=indexPath.row;
if (selectstr ==indexPath.row)
{
[button setImage:[UIImage imageNamed:@"radio_selected.png"] forState:UIControlStateNormal];
cell.backgroundColor=[UIColor colorWithRed:(243.0/255.0) green:(114.0/255.0) blue:(74.0/255.0) alpha:1.0f];
}
else
{
[button setImage:[UIImage imageNamed:@"radio_unselected.png"] forState:UIControlStateNormal];
cell.backgroundColor=[UIColor clearColor];
}
button.adjustsImageWhenHighlighted=NO;
[button addTarget:self action:@selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectstr=indexPath.row;
[tableView reloadData];
}
- (IBAction)toggleCheckedMode:(UIButton*)sender
{
[button setImage:[UIImage imageNamed:@"radio_selected.png"] forState:UIControlStateNormal];
// here customized for your self
selectstr=sender.tag;
[tableView reloadData];
}
+0
試試這個,這是工作正常,我 – 2014-09-03 11:12:40
+0
感謝您的答覆。是的,它在didselect中運行良好。但我在單擊單選按鈕上尋找相同的功能。 – 2014-09-03 11:38:49
+0
好的,謝謝... :) – 2014-09-03 11:44:26
0
Write this code into
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSIndexPath *selectedIndex;
UIButton *radioButton;
radioButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
radioButton.frame = CGRectMake(30,23,24,24);
// [radioButton setTitle:@"" forState:UIControlStateNormal];
radioButton.tag = 8888;
[cell.contentView addSubview:radioButton];
radioButton = (UIButton *)[cell.contentView viewWithTag:8888];
if (selectedIndex && selectedIndex.row == indexPath.row)
{
[radioButton setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
}
else
{
[radioButton setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex =indexPath;
[self.userTabelView reloadData];
}
相關問題
- 1. 單選按鈕不工作
- 2. 按鈕裏面的GridView裏面更新面板不工作
- 3. 奧裏利亞轉發:model.bind不工作的單選按鈕
- 4. 按鈕點擊不工作裏面gridview裏面updatepanel
- 5. Asp.net按鈕裏面控件裏面gridview不工作
- 6. HTML表單單選按鈕不工作
- 7. 單選按鈕不工作的JQuery
- 8. UITableView滾動不工作在裏面UIScrollView
- 9. 單選按鈕工作
- 10. PHP單選按鈕裏面回聲
- 11. 表裏面如果單選按鈕ID
- 12. 組名不超過一個單選按鈕轉發asp.net裏面工作
- 13. HTML單選按鈕CSS不工作
- 14. javascript自制單選按鈕不工作
- 15. 單選按鈕在Firefox中不工作
- 16. 單選按鈕檢查不工作
- 17. JQueryUI單選按鈕示例不工作
- 18. 單選按鈕不Vue.js正常工作
- 19. 單選按鈕不工作 - AngularJS
- 20. WPF單選按鈕IsChecked不工作
- 21. 單選按鈕在asp.net中不工作
- 22. 驗證單選按鈕不工作jquery
- 23. 單選按鈕不工作在jquery innerhtml
- 24. jquery單選按鈕每個不工作
- 25. Javascript onclick單選按鈕不工作
- 26. javasscript單選按鈕不工作?
- 27. 提交按鈕裏面的Android單選按鈕?
- 28. 單選按鈕不工作(可同時選擇多個單選按鈕)
- 29. 引導按鈕單選按鈕顯示裏面列
- 30. UITableView footerView按鈕,按鈕不起作用
添加關於你的單選按鈕,小區一些代碼 – raki 2014-09-03 11:11:08