2

我创建了下面的代码来显示按钮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 {}
4

1 回答 1

8

尝试类似:

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

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

如果您不想打开 safari,则应显示自己的UIWebView.

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

[cell.contentView addSubview:button];
于 2012-07-01T10:31:04.913 回答