我知道这不是一个非常先进的问题,但我感到迷茫。我有一个定义自定义表格单元格样式的类,其中包含我想要编辑的所需按钮。然后在另一个类中,我使用这个单元格类型并构造一个表视图。然后我写了IBAction
到不同班级的按钮。点击按钮时,它应保持选中状态。我已经尝试了几件事,但我无法正确地获得按钮的参考以提供所需的结果。
我仍在尝试适应Objective-C。
customcellstyle.m
#import "imageCellCell.h"
@implementation imageCellCell
@synthesize view;
@synthesize starbtn;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate favorites button
starbtn = [[UIButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];
[starbtn setTintColor:[UIColor clearColor]];
[starbtn setBackgroundImage:[UIImage imageNamed:@"startbefore.jpg"]
forState:UIControlStateNormal];
[starbtn setBackgroundImage:[UIImage imageNamed:@"startafter.jpg"]
forState:UIControlStateSelected];
[starbtn setBackgroundImage:[UIImage imageNamed:@"startafter.jpg"]
forState:UIControlStateHighlighted];
[starbtn setBackgroundImage:[UIImage imageNamed:@"startafter.jpg"]
[view addSubview:starbtn];
forState:UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
tableclass.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier;
NSString *CellIdentifierimg;
UITableViewCell *cell;
if (cell == nil) {
if (indexPath.row == 0) {
cell = [[imageCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierimg];
} else if (indexPath.row == 1) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
switch ([indexPath row])
{
case 0:
{
imageCellCell *firstRowCell = (imageCellCell *)cell;
// reference of the favorites button to the buttonclick method
[firstRowCell.starbtn addTarget:self action:@selector(clickFavButton:) forControlEvents:UIControlEventTouchUpInside];
firstRowCell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
-(IBAction)clickFavButton:(id)sender{
customcellstyle *class = [[customcellstyle alloc] init];;
[class.starbtn setSelected:YES];
[class.starbtn setHighlighted:YES];
[starbtn setSelected:YES];
[starbtn setHighlighted:YES];
}
答案 0 :(得分:1)
您应该创建一个自定义按钮类,您需要使用它选择的值来设置和处理按钮图像,我已经为复选框按钮做了同样的事情,如
<。>文件中的
@interface MXCheckButton : UIButton {
BOOL _checked;
}
@property (nonatomic, setter = setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
@end
并在您的.m文件中
@implementation MXCheckButton
@synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
self.checked = NO;
[self addTarget:self action:@selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:@selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:@"checked.png"];
[self setImage:img forState:UIControlStateNormal];
}
else
{
UIImage* img = [UIImage imageNamed:@"unchecked.png"];
[self setImage:img forState:UIControlStateNormal];
}
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
@end
在这里你只需更改图像文件名即可开始使用!