2014-07-11 73 views
0

我有一個UIButton子類,它定製了一個按鈕。然後我有一個UITableViewCell子類,它使用此按鈕並將其放置在單元格中。最後,我有一個UITableViewController的子類,它顯示錶視圖幷包含自定義的單元格。我的問題是,定製按鈕「starbtn」不會顯示在表格中,而其他一切正常。我的代碼有什麼問題?按鈕不會出現在表格單元格中

favButton.h

#import <UIKit/UIKit.h> 

@interface favButton : UIButton { 
    BOOL _checked; 
} 

@property (nonatomic, setter = setChecked:) BOOL checked; 

-(void) setChecked:(BOOL) check; 

@end 

favButton.m

#import "favButton.h" 

@implementation favButton 

@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) 
    { 
     [self setTintColor:[UIColor clearColor]]; 
     UIImage* img = [UIImage imageNamed:@"startbefore.jpg"]; 
     [self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled]; 

    } 
    else 
    { 
     [self setTintColor:[UIColor clearColor]]; 
     UIImage* img = [UIImage imageNamed:@"startafter.jpg"]; 
     [self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled]; 

    } 
} 

-(void) OnCheck:(id) sender 
{ 
    self.checked = !_checked; 
} 


@end 

imageCellCell.h

#import <UIKit/UIKit.h> 
#import "favButton.h" 

    @interface imageCellCell : UITableViewCell 

    @property (nonatomic, strong) UIView *view; 
    @property (nonatomic, strong) UILabel *label1; 
    @property (nonatomic, strong) UILabel *label2; 
    @property (nonatomic, strong) UIImageView *prodimage; 
    @property (nonatomic, strong) UIImageView *thumbsup; 
    @property (nonatomic, strong) UILabel *label3; 
    @property (nonatomic, strong) UIImageView *basket; 
    @property (nonatomic, strong) UIButton *homebtn; 

    @property (nonatomic, strong) favButton *starbtn; 

    @end 

imageCellCell.m

#import "imageCellCell.h" 




@implementation imageCellCell 

@synthesize view; 
@synthesize label1; 
@synthesize label2; 
@synthesize prodimage; 
@synthesize thumbsup; 
@synthesize label3; 
@synthesize basket; 
@synthesize homebtn; 
@synthesize starbtn; 



- (instancetype)init { 

    self = [super init]; 
    self.starbtn = [[favButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)]; 

    [starbtn setTintColor:[UIColor clearColor]]; 

    [starbtn setBackgroundImage:[UIImage imageNamed:@"startbefore.jpg"] 
         forState:UIControlStateNormal]; 
    return self; 
} 

- (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 image 
     prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)]; 

     // initiate label1 score value 
     label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)]; 
     label1.textColor = [UIColor greenColor]; 

     [[self label1] setFont:[UIFont systemFontOfSize:25]]; 


     // initiate label2 score label 
     label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)]; 

     // initiate image 
     thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)]; 

     // initiate label3 manufacturer 

     label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)]; 

     // initiate image 
     basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)]; 


     // initiate home button  
     homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)]; 


     [homebtn setTintColor:[UIColor clearColor]]; 
     [homebtn setBackgroundImage:[UIImage imageNamed:@"home.jpg"] 
          forState:UIControlStateNormal]; 


     [view addSubview:prodimage]; 
     [view addSubview:label1]; 
     [view addSubview:label2]; 
     [view addSubview:thumbsup]; 
     [view addSubview:label3]; 
     [view addSubview:basket]; 
     [view addSubview:homebtn]; 
     [view addSubview:starbtn]; 

    } 
    return self; 
} 

@end 

ProdViewController.m

#import "ProdViewController.h" 
#import "imageCellCell.h" 


@interface ProdViewController() 

@end 

@implementation ProdViewController 
@synthesize tableview; 
@synthesize tabbar; 
@synthesize addfav; 
@synthesize favData; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    favData = [[NSMutableArray alloc] init]; 
    [tableview setDataSource:self]; 
    [tableview setDelegate:self]; 


    } 

- (void)viewDidUnload 
{ 
    [self setTableview:nil]; 
    [self setTabbar:nil]; 
    [self setAddfav:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 


... 


- (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]; 
     }else if (indexPath.row == 2) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     }else if (indexPath.row == 3) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     }else if (indexPath.row == 4) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 
    } 




    switch ([indexPath row]) 
    { 
     case 0: 
     { 

      imageCellCell *firstRowCell = (imageCellCell *)cell; 


      [firstRowCell.thumbsup setImage: [UIImage imageNamed:@"thumbs-up-green.jpg"]]; 

      [firstRowCell.prodimage setImage: [UIImage imageNamed:@"test1.jpg"]]; 
      [firstRowCell.label1 setText:@"10"]; 
      [firstRowCell.label2 setText:@"Score"]; 
      [firstRowCell.label3 setText:@"Name"]; 

      [firstRowCell.basket setImage: [UIImage imageNamed:@"Basket.jpg"]]; 

      // reference of the home button to the buttonclick method 

      [firstRowCell.homebtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside]; 

      // 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; 


     } 
     case 1: 
     { 


       cell.textLabel.text = @"Row 2"; 
       cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 

      break; 

     } 

     case 2: 
     { 
      cell.textLabel.text = @"Row 3"; 
      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 


      break; 

     } 

     case 3: 
     { 
      cell.textLabel.text = @"Row 4"; 
      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 


      break; 


     } 

      } 

    return cell; 

} 



-(IBAction)clickFavButton:(id)sender{ 

[favData addObject:@"productname"]; 



} 



@end 
+0

看看我的新編輯答案 –

回答

1

爲什麼ü被添加在init方法的starbtn,把這些代碼在- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

註釋掉整個init方法

- (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 image 
    prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)]; 

    // initiate label1 score value 
    label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)]; 
    label1.textColor = [UIColor greenColor]; 

    [[self label1] setFont:[UIFont systemFontOfSize:25]]; 


    // initiate label2 score label 
    label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)]; 

    // initiate image 
    thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)]; 

    // initiate label3 manufacturer 

    label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)]; 

    // initiate image 
    basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)]; 


    // initiate home button  
    homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)]; 


    [homebtn setTintColor:[UIColor clearColor]]; 
    [homebtn setBackgroundImage:[UIImage imageNamed:@"home.jpg"] 
         forState:UIControlStateNormal]; 

    //initilise hear 
    //edit 
    starbtn = [[favButton alloc]init]; 
    starbtn.frame = CGRectMake(243,0, 30, 30)]; 
    //edit 
    [starbtn setTintColor:[UIColor clearColor]]; 

    [starbtn setBackgroundImage:[UIImage imageNamed:@"startbefore.jpg"] 
        forState:UIControlStateNormal]; 

    [view addSubview: starbtn]; //add this 

    [view addSubview:prodimage]; 
    [view addSubview:label1]; 
    [view addSubview:label2]; 
    [view addSubview:thumbsup]; 
    [view addSubview:label3]; 
    [view addSubview:basket]; 
    [view addSubview:homebtn]; 
    [view addSubview:starbtn]; 

} 
    return self; 
} 

編輯答案

"favButton.h"文件

#import <UIKit/UIKit.h> 
    @interface favButton : UIButton 
    { 
    BOOL _checked; 
    } 
    @property (nonatomic, setter = setChecked:) BOOL checked; 
    -(void) setChecked:(BOOL) check; 

    @end 

"favButton.m"文件

imageCellCell.h文件
#import "favButton.h" 

@implementation favButton 

- (id)initWithFrame:(CGRect)frame 
    { 
    self = [super initWithFrame:frame]; 
    if (self) { 
      // Initialization code 
      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) 
    { 
     // [self setTintColor:[UIColor clearColor]]; 
      UIImage* img = [UIImage imageNamed:@"22.jpg"]; 
     //  [self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled]; 
     [self setImage:[UIImage imageNamed:@"22.jpg"] forState:UIControlStateNormal]; 

     } 
     else 
     { 
     // [self setTintColor:[UIColor clearColor]]; 
     UIImage* img = [UIImage imageNamed:@"33.jpg"]; 
     // [self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled]; 
     [self setImage:[UIImage imageNamed:@"33.jpg"] forState:UIControlStateNormal]; 
    } 
    } 

    -(void) OnCheck:(id) sender 
    { 
    self.checked = !_checked; 
    } 


/* 
// Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
    { 
    // Drawing code 
    } 
*/ 

@end 

僅包括這些方法

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    // Initialization code 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     view = [[UIView alloc] initWithFrame:self.frame]; 
     [self addSubview:view]; 

     // initiate image 
     prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)]; 

     // initiate label1 score value 
     label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)]; 
     label1.textColor = [UIColor greenColor]; 

     [[self label1] setFont:[UIFont systemFontOfSize:25]]; 


     // initiate label2 score label 
     label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)]; 

     // initiate image 
     thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)]; 

     // initiate label3 manufacturer 

     label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)]; 

     // initiate image 
     basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)]; 


     // initiate home button 
     homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)]; 


     [homebtn setTintColor:[UIColor clearColor]]; 
     [homebtn setBackgroundImage:[UIImage imageNamed:@"home.jpg"] 
          forState:UIControlStateNormal]; 
     starbtn = [[favButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)]; 
     starbtn.backgroundColor = [UIColor greenColor]; 
     // starbtn.frame = CGRectMake(243,0, 30, 30); 

     //edit 
     [starbtn setTintColor:[UIColor clearColor]]; 

     [starbtn setBackgroundImage:[UIImage imageNamed:@"startbefore.jpg"] 
          forState:UIControlStateNormal]; 

     [view addSubview: starbtn]; //add this 

     [view addSubview:prodimage]; 
     [view addSubview:label1]; 
     [view addSubview:label2]; 
     [view addSubview:thumbsup]; 
     [view addSubview:label3]; 
     [view addSubview:basket]; 
     [view addSubview:homebtn]; 
     [view addSubview:starbtn]; 

    } 
    return self; 
    } 

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated 
    { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
    } 

    @end 

在控制器都是相同的,需要改變

末編輯

希望這有助於ü.. :)

+0

謝謝您的答覆。我已經嘗試過這種方式,但是我的自定義按鈕類使得uibutton充當複選框,並根據選擇的內容確定顯示的圖像。如果按照你所建議的方式進行操作,則不會應用圖像中解釦子類的更改。 – user3211165

+0

嘗試編輯的答案... –

+0

我已經將btn包含到了子視圖中。它不工作。如果我包含這行[homebtn setBackgroundImage:[UIImage imageNamed:@「home.jpg」] forState:UIControlStateNormal];這就像我忽略了子類方法,所以圖像在鼠標點擊時不能正確更改。但即使我刪除它,它也不會出現。 – user3211165

相關問題