带有网络链接的单元格中的按钮

Posted

技术标签:

【中文标题】带有网络链接的单元格中的按钮【英文标题】:button in cell with weblink 【发布时间】:2014-03-25 21:24:14 【问题描述】:

我正在尝试向具有 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

【问题讨论】:

【参考方案1】:

创建单元格时只需将目标添加到按钮操作

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

【讨论】:

工作完美。我是 ios 新手,所以只称它为 cell.homelink 没有括号。

以上是关于带有网络链接的单元格中的按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UITableViewCell 单元格中隐藏具有大小的按钮?

将 xib 创建的单元格中的按钮连接到选项卡式视图应用程序中的视图

如何将操作按钮添加到 collectionView 的部分单元格中?

单元格中的选定按钮显示在重用单元格上 - 也使用 sender.tag 进行按钮区分

如何使用ruby中的电子表格gem从excel单元格中提取超链接地址?

如何在单个单元格中插入多个超链接?