2014-03-25 52 views
0

我正在尝试将一个简单的按钮添加到具有网页链接的自定义单元格。我在故事板中添加了按钮并将其与houseLink关联。这里是我的view.M文件,我称之为自定义单元格中的按钮。带网页链接的单元格中的按钮

#import "IBSecondViewController.h" 
    #import "C21TableCell.h" 


    @interface IBSecondViewController() 

    @end 

    @implementation IBSecondViewController 
    { 
     NSArray *thumbnails; 

    } 
    @synthesize data; 

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

     //Initialize House Array 
     data = [NSArray arrayWithObjects:@"2109 E Avon Cricle, Hayden Lake, ID", @"703 E Garden, Coeur d' Alene, ID", nil]; 

     _bedroom = [NSArray arrayWithObjects:@"3", @"4", nil]; 

     // Initialize thumbnails 
     thumbnails = [NSArray arrayWithObjects:@"stockHouse.png", @"stockHouse2.png", nil]; 

    } 

    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

    #pragma mark - Table view ddata source 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return 1; 
    } 

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return [data count]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *simpleTableIdentifier = @"C21TableCell"; 

     C21TableCell *cell = (C21TableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"C21TableCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 

     cell.addressLabel.text = [data objectAtIndex:indexPath.row]; 
     cell.bedroomLabel.text = [_bedroom objectAtIndex:indexPath.row]; 
     cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]]; 
NOT SURE HOW TO CALL THE BUTTON HERE 



     return cell; 
    } 

    -(void) buttonpressed:(UIButton *)sender { 
     NSString* launchUrl = @"http://apple.com/"; 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]]; 
    } 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return 78; 
    } 

    @end 

C21TableCell.H

#import <UIKit/UIKit.h> 

@interface C21TableCell : UITableViewCell 

@property (nonatomic, weak) IBOutlet UILabel *addressLabel; 
@property (nonatomic, weak) IBOutlet UILabel *bedroomLabel; 
@property (nonatomic, weak) IBOutlet UIImageView *thumbnailImageView; 
@property (nonatomic, weak) IBOutlet UIButton *homeLink; 


@end 

C21TableCell.M

#import "C21TableCell.h" 

@implementation C21TableCell 

@synthesize addressLabel = _nameLabel; 
@synthesize bedroomLabel = _bedroomLabel; 
@synthesize thumbnailImageView = _thumbnailImageView; 
@synthesize homeLink = homeLink; 


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

- (void)awakeFromNib 
{ 
    // Initialization code 
} 

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

    // Configure the view for the selected state 
} 

@end 

回答

0

只需添加目标按钮动作,当你创建细胞

[cell.homeLink addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside]; 
+0

曾任职完美。我是iOS新手,所以只是把它叫做'cell.homelink'没有括号。 – Packy