2014-03-25 99 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