繁体   English   中英

如何在UITableView中打开UIButton的链接

[英]How to open a link with a UIButton within a UITableView

我创建了以下代码来显示UITableView按钮。 我想为表格中的每个特定单元格添加点击事件,以通过点击该按钮访问Google链接。 如何将点击事件添加到特定单元格的按钮选项中并进入谷歌链接?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"cell";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(5,5,40,40)];
    [button addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"lori"] forState:UIControlStateNormal];
    [button setTag:9001];
    [cell addSubview:button];
    [cell setIndentationWidth:45];
    [cell setIndentationLevel:1];
    cell.textLabel.text = [thearray objectAtIndex:indexPath.row];

    return cell;
}

-(void) buttonpressed:(UIButton *)sender {}

尝试类似的东西:

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

这将打开指向您传入的URL的safari。

如果您不想打开safari,那么您应该显示自己的UIWebView

此外,您可能希望将按钮添加到contentView

[cell.contentView addSubview:button];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM