如何在iOS的UITableView单元格中将标记值设置为UIButton?
我使用了自定义单元格并在如何在iOS的UITableView单元格中将标记值设置为UIButton?,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我使用了自定义单元格并在UITableViewCell中创建了UIButton。我想更改按钮状态,如下屏幕截图所示。我是新来Xcode的,有人能给我指点路吗。提前谢谢 这是我的密码 PrivacyCell.h @property (strong, nonatomic) IBOutlet UIButton *on_offBtn; - (IBAction)on_offBtnAction:(UIButton *)sender; @property (nonatomic,assign) BOOL
UITableViewCell
中创建了UIButton
。我想更改按钮状态,如下屏幕截图所示。我是新来Xcode的,有人能给我指点路吗。提前谢谢
这是我的密码
PrivacyCell.h
@property (strong, nonatomic) IBOutlet UIButton *on_offBtn;
- (IBAction)on_offBtnAction:(UIButton *)sender;
@property (nonatomic,assign) BOOL on_image;
@property (nonatomic,assign) BOOL off_image;
PrivacyCell.m
#import "PrivacyCell.h"
- (IBAction)on_offBtnAction:(UIButton *)sender {
if (on_image == YES) {
[_on_offBtn setImage:[UIImage imageNamed:@"on_icon.png"] forState:UIControlStateNormal];
on_image = NO;
} else if (on_image == NO) {
[_on_offBtn setImage:[UIImage imageNamed:@"off_icon.png"] forState:UIControlStateNormal];
on_image = YES;
}
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self)
{
on_offBtn.tag = 100;
}
return self;
}
在
cellforrowatinexpath
中设置按钮属性,如
btn.tag = indexPath.row;
[btn setImage:[UIImage imageNamed:@"off_btn.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"on_btn.png"] forState:UIControlStateSelected];
并在单击事件中更改其状态
- (IBAction)on_offBtnAction:(UIButton *)sender
{
sender.selected = !sender.selected;
// [yourTableVew reloadData];
}
在
cellforrowatinexpath
中设置按钮属性,如
btn.tag = indexPath.row;
[btn setImage:[UIImage imageNamed:@"off_btn.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"on_btn.png"] forState:UIControlStateSelected];
并在单击事件中更改其状态
- (IBAction)on_offBtnAction:(UIButton *)sender
{
sender.selected = !sender.selected;
// [yourTableVew reloadData];
}
你可以在这里设置标签
PrivacyCell.m
#import "PrivacyCell.h"
- (IBAction)on_offBtnAction:(UIButton *)sender {
if (on_image == YES) {
[_on_offBtn setImage:[UIImage imageNamed:@"on_icon.png"] forState:UIControlStateNormal];
on_image = NO;
} else if (on_image == NO) {
[_on_offBtn setImage:[UIImage imageNamed:@"off_icon.png"] forState:UIControlStateNormal];
on_image = YES;
}
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self)
{
on_offBtn.tag = 100;
}
return self;
}
然后你可以使用
UIButton *button = (UIButton *)[cell viewWithTag:100];
button.selected = true;
但是您也可以使用您的属性。您可以在此处设置标记
PrivacyCell.m
#import "PrivacyCell.h"
- (IBAction)on_offBtnAction:(UIButton *)sender {
if (on_image == YES) {
[_on_offBtn setImage:[UIImage imageNamed:@"on_icon.png"] forState:UIControlStateNormal];
on_image = NO;
} else if (on_image == NO) {
[_on_offBtn setImage:[UIImage imageNamed:@"off_icon.png"] forState:UIControlStateNormal];
on_image = YES;
}
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self)
{
on_offBtn.tag = 100;
}
return self;
}
然后你可以使用
UIButton *button = (UIButton *)[cell viewWithTag:100];
button.selected = true;
但您也可以使用您的属性。甚至您可以在
cellforrowatinexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
PrivacyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[PrivacyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// or you can use custom method for init
}
...
cell.on_offBtn.tag = indexPath.row;
...
return cell;
}
甚至您也可以在
cellforrowatinexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
PrivacyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[PrivacyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// or you can use custom method for init
}
...
cell.on_offBtn.tag = indexPath.row;
...
return cell;
}
您可以从故事板中为UITableViewCell中的按钮创建iAction,然后每当您单击该按钮时,将调用此方法。您将获得所单击按钮的索引,您可以相应地执行任何任务 如果选项卡上有多个按钮,这会有所帮助
- (IBAction)tidesButtonAction:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero
toView:self.tableView];
NSIndexPath *tappedIP = [self.tableView indexPathForRowAtPoint:buttonPosition];
NSString *text = [tidesURLArray objectAtIndex:tappedIP.row];
}
您可以从故事板中为UITableViewCell中的按钮创建iAction,然后每当您单击该按钮时,将调用此方法。您将获得所单击按钮的索引,您可以相应地执行任何任务 如果选项卡上有多个按钮,这会有所帮助
- (IBAction)tidesButtonAction:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero
toView:self.tableView];
NSIndexPath *tappedIP = [self.tableView indexPathForRowAtPoint:buttonPosition];
NSString *text = [tidesURLArray objectAtIndex:tappedIP.row];
}
如何填充UITableView?请添加
cellForRow
part显示cellforrowatinexpath
如何填充UITableView?请添加cellForRow
part显示cellforrowatinexpath
完整的解决方案在这里。请参阅此链接我正在使用CustomCell显示btn。我应该如何设置[btn setImage:[UIImage ImageName:@“off_btn.png”]状态:UIControlStateNormal];[btn setImage:[UIImage ImageName:@“on_btn.png”]用于状态:UIControlStateSelected];完整的解决方案就在这里。请参阅此链接我正在使用CustomCell显示btn。我应该如何设置[btn setImage:[UIImage ImageName:@“off_btn.png”]状态:UIControlStateNormal];[btn setImage:[UIImage ImageName:@“on_btn.png”]用于状态:UIControlStateSelected];