ios - 如何在 UITableView 中打开带有 UIButton 的链接

标签 ios uitableview hyperlink uibutton

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

- (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];

关于ios - 如何在 UITableView 中打开带有 UIButton 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11280994/

相关文章:

ios - CLLocationManager区域监控委托(delegate)类问题

iphone - UITableViewCell ClipsToBound 不起作用

linux - MinGW 上的 libbfd 缺少哪些库

swift - 如何在使用 Kingfisher 从 URL 加载图像后调整 UIImageView 的大小

ios - 确定在哪个表格 View 中按下了 Cell 按钮?

php - 如何在本地主机和 Web 服务器上实现绝对 URL?

javascript - 使用 jQuery/JavaScript 从超链接获取完整 URL

ios - 网络连接从 Alamofire 丢失

ios - Tableview 未收到来自驱动程序的信号

ios - 在 swift 3 中预览操作项