在Tableview中添加单选按钮有一些问题 [英] Add Radio Button in Tableview Got Some Problem
问题描述
我知道xcode没有单选按钮
I know xcode don't have radio Button
,所以我尝试添加一个自定义按钮,使其像一个单选按钮
so I try to add a custom button and make it action like a radio button
这是我使用的图片
This is the image I use
这是我设置的代码到单元格
and this is the code I set to cell
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[but setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
[but setFrame:CGRectMake(0, 0, 44, 44)];
[but addTarget:self action:@selector(radioButton:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView= but;
这是我要问的问题是
如何在 - (IBAction)radioButton:(UIButton *)按钮中提供空格
如何控制两行中的两个单选按钮
To control Two Radio Button in Two Rows
如果选择行1单选按钮为YES
If Row 1 Radio button's selected is YES
btn将会 btn.state = NO
,并且不会回应
btn in row 2 will be btn.state=NO
and won't response the
- )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
就像这张图片
It will be like this pic
如何在<$ c中设置if条件$ c> - (IBAction)radioButton:(UIButton *)button
这个pic是假的...我只添加cell ...并更改文本颜色
this pic is fake...I only add the button in the cell...and changed the text color
非常感谢所有堆栈溢出朋友〜
Great Thanks to all stack overflow friends~
推荐答案
您将需要一个所有单选按钮的数组。记住,表格单元格被回收/可能不可见等,所以创建一个数组只是与按钮,然后抓住该数组在 tableView:cellForIndexPath:
方法。
You will need an array of all the radio buttons. Remember that table cells get recycled/may not be visible, etc. so create an array just with the buttons and then grab the right button out of that array in your tableView:cellForIndexPath:
method.
所以在你的 tableView:cellForIndexPath:
方法中你会做这样的:
So in your tableView:cellForIndexPath:
method you would do something like this:
cell.accessoryView = [myButtonArray objectAtIndex:[indexPath row]];
然后,在 radioButton:
radioButtonPressed:
方法:
// Select the pressed button.
[button setSelected:YES];
// Unselect all others.
for (UIButton *other in myButtonArray) {
if (other != button) {
[other setSelected:NO];
}
}
这篇关于在Tableview中添加单选按钮有一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!