1
A
回答
0
通过SO链接。
Checkbox cell in a table view: User can't check it
checkbox button in table view in iphone
更多阅读苹果文档的在UITableViewCell
管理选择。
编辑:的UIButton
使用标签属性,
#define KON 1 //For checked state
#define KOFF 0 //For Unchecked state
-(void) myButtonAction:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton.tag == KON)
{
myButton.tag = KOFF;
//Change the button image
}
else
{
myButton.tag = KON;
//Change the button image
}
}
0
例如,单击第一行是用来更改其他行。点击其他行只会改变它自己。 arrCheckbox是一个数组,用于存储所有行的检查。
- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"AV--%s", __func__);
HistoryCell2 *cell = [tableview dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
NSLog(@"Create new row");
[tableview registerNib:[UINib nibWithNibName:@"HistoryCell2" bundle:nil] forCellReuseIdentifier:CellId];
cell = [tableview dequeueReusableCellWithIdentifier:CellId];
}
cell.tag = indexPath.row;
BOOL checked = [[arrCheckbox objectAtIndex:indexPath.row] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"cb_on.png"] : [UIImage imageNamed:@"cb_off.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(1.0, 1.0, 29, 29);
button.frame = frame; // match the button's size with the image size
button.tag = indexPath.row;
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
- (void) checkButtonTapped:(id)sender
{
NSLog(@"%s", __func__);
UIButton *tappedButton = (UIButton*)sender;
int index = (int)tappedButton.tag;
NSLog(@"Row button: %d", index);
if(index > 0)
{
BOOL checked = [[arrCheckbox objectAtIndex:index] boolValue];
[arrCheckbox removeObjectAtIndex:index];
[arrCheckbox insertObject:(checked) ? @"0":@"1" atIndex:index];
UIImage *newImage = (checked) ? [UIImage imageNamed:@"cb_off.png"] : [UIImage imageNamed:@"cb_on.png"];
[tappedButton setBackgroundImage:newImage forState:UIControlStateNormal];
}
else{
//UITableView *tableview = (UITableView *)[[tappedButton superview] superview];
UIImage *newImage;
BOOL checked = [[arrCheckbox objectAtIndex:0] boolValue];
for(int i=0; i<[arrCheckbox count]; i++)
{
//NSLog(@"Row: %d------", i);
[arrCheckbox removeObjectAtIndex:i];
[arrCheckbox insertObject:(checked) ? @"0":@"1" atIndex:i];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
for(UIView *subview in cell.contentView.subviews)
{
if([subview isKindOfClass: [UIButton class]])
{
//NSLog(@"Modify button");
tappedButton = (UIButton*)subview;
//[subview removeFromSuperview];
newImage = (checked) ? [UIImage imageNamed:@"cb_off.png"] : [UIImage imageNamed:@"cb_on.png"];
[tappedButton setBackgroundImage:newImage forState:UIControlStateNormal];
}
}
}
}
}
相关问题
- 1. UITableView中的复选框
- 2. 如果选中表格复选框,取消选中其他表格复选框?
- 3. 表格中的复选框选择
- 4. 复选框覆盖的UITableView
- 5. 表格和值中的复选框
- 6. Qt - 访问表格中的复选框
- 7. 表格行中的ngrepeat复选框
- 8. MediaWiki中带有复选框的表格
- 9. HTML复选框表格php
- 10. 当单击一个复选框时选中表格中的所有复选框
- 11. 使用VBA设置表格中的复选框不是表格
- 12. 复选框与不同部分的UITableView
- 13. AngularJS:重复并从表格中的复选框中获取值
- 14. 定位表格内的复选框
- 15. 带复选框的简单表格php
- 16. 获取html表格中已选中的复选框
- 17. MDL在表格中添加选中的复选框
- 18. 如何检查表格中的复选框是否已选中
- 19. UITableView中的复选框更改和恢复图像
- 20. 使用AngularJS的表格中的复选框选择
- 21. 复选框列表在IE11中的表格中未呈现
- 22. 如何添加表格视图单元格中的复选框?
- 23. 网格复选框选择
- 24. Xpath选择生成表格的Div中前同步复选框
- 25. 我想获取选中复选框的表格发票ID
- 26. javascript选择表格中的所有复选框
- 27. 如何根据行值选择表格中的复选框?
- 28. 预选mysql表格中的复选框数组
- 29. angularjs格式的复选框
- 30. Yii2 Grdiview复选框列通过表格
如何在表 – sujay 2011-04-21 10:20:50
@sameer创建3个复选框3行:添加一个UIButton的每个细胞, – Jhaliya 2011-04-21 10:22:23
@sameer:接受,如果我的回答可以帮助您。 – Jhaliya 2011-04-23 17:52:50