![](/img/trans.png)
[英]How to hide a particular row button in Dynamic UITableViewCell in Objective C?
[英]how to add checkboxes button in UITableViewCell objective c
我在这里知道这个问题的很多答案,但我无法理解所有人的正确含义。
我想按钮选择而不是单元格。 默认情况下按钮未选中,如果单击或按下按钮,则按钮更改为选中(仅按下按钮)而不是全部。
我提到了我的屏幕截图和代码,有按钮、数组的值(设备名称)和带有开关按钮的东西
代码
-(IBAction)SelectAppliance:(id)sender {
UIButton *btn=(UIButton*)sender;
NSLog(@"button tag is :%ld",(long)btn.tag);
NSLog(@"click button");
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_arrApplnc count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SceneTableViewCell";
SceneTableViewCell *cell = (SceneTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(checked) {
[cell.btnRadio setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
} else {
[cell.btnRadio setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
}
cell.btnRadio.tag = indexPath.row;
cell.lblName.text =[[_arrApplnc valueForKey:@"applianceName"] objectAtIndex:indexPath.row];
[cell.btnRadio addTarget:self action:@selector(SelectAppliance:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
如果您打算在单元格中使用按钮,我建议使用委托模式,以便单元格本身处理按钮点击并向视图控制器发送消息。
在SceneTableViewCell.h
@protocol SceneTableViewCellDelegate <NSObject>
@optional
- (void)checkBoxTapped:(SceneTableViewCell *)cell;
@end
@interface SceneTableViewCell : NSObject
@property (nonatomic, weak) id <SceneTableViewCellDelegate> delegate;
@end
在SceneTableViewCell.m
-(IBAction)checkboxTapped:(UIButton *)sender {
[self.delegate checkBoxTapped:self];
}
在你的ViewController.m
@property (nonatomic,strong) NSMutableIndexSet *checkedRows;
- (void) viewDidLoad {
self.checkedRows = [[NSMutableIndexSet alloc]init];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SceneTableViewCell";
SceneTableViewCell *cell = (SceneTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.delegate = self
}
if([self.checkedRows contains:indexPath.row]) {
[cell.btnRadio setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
} else {
[cell.btnRadio setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
}
cell.lblName.text =[[_arrApplnc valueForKey:@"applianceName"] objectAtIndex:indexPath.row];
return cell;
}
- (void) checkBoxTapped:(SceneTableView Cell *)cell {
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
if ([self.checkedRows contains: indexPath.row]) {
[self.checkedRows removeIndex: indexPath.row];
} else {
[self.checkedRows addIndex: indexPath.row];
}
[tableView reloadRowsAt:@[indexPath], withRowAnimation: UITableViewRowAnimationAutomatic];
}
另一种方法是简单地在单元格中使用UIImageView
而不是UIButton
并使用didSelect
处理程序来切换选中状态。 这样用户就可以点击单元格中的任何地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.