【发布时间】:2014-07-10 11:00:02
【问题描述】:
我知道这不是一个非常高级的问题,但我感到迷茫。我有一个定义自定义表格单元格样式的类,其中包含我希望编辑的所需按钮。然后在另一个类中,我使用这种单元格类型并构造一个表格视图。然后我在不同类的按钮上写一个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];
}
【问题讨论】:
标签: ios xcode uitableview uibutton