我正在尝试将一个简单的按钮添加到具有Web链接的自定义单元格中。我在故事板中添加了按钮并将其与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 :(得分:0)
创建单元格时,只需将目标添加到按钮操作
[cell.homeLink addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];