如何在UITableView中打开UIButton的链接

Hit*_*esh 2 hyperlink uibutton uitableview ios

我创建了以下代码来显示按钮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 {}
Run Code Online (Sandbox Code Playgroud)

ser*_*gio 8

尝试类似的东西:

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

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

如果你不想打开safari,那么你应该展示自己的UIWebView.

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

[cell.contentView addSubview:button];
Run Code Online (Sandbox Code Playgroud)