iphone - 在行选择处添加 uiview

标签 iphone ios ipad uitableview uiview

我希望当行上的选项卡上出现一个包含两个按钮的 UIView 时,单元格的中心出现,所以我做了以下代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //how can I get the text of the cell here?
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    UIView *v = [[UIView alloc] init];

    v.center  = cell.center;

    UIButton*button1 =[[UIButton alloc] init];

    button1.frame = CGRectMake(v.center.x - 5 , v.center.y  , 5 , v.center.y + 4  );
    button1.titleLabel.text = @"I'm label 1";

    [v addSubview: button1];

    UIButton*button2 =[[UIButton alloc] init];

    button2.frame = CGRectMake(v.center.x - + 1 , v.center.y  , 5 , v.center.y + 4  );
    button2.titleLabel.text = @"I'm label 2";

    [v addSubview: button2];

    [cell addSubview:v];

    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}
UIView 没有出现。如何解决?

最佳答案

尝试:

[cell.contentView addSubview: v];

它应该工作。

编辑:
试试这个代码。相应地给框架,它会工作。
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(44, 100, 200, 200)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 50, 50);
[v addSubview:button];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:v];

关于iphone - 在行选择处添加 uiview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9461487/

相关文章:

ios - 在iPAD上检测iPhone特定的应用程序“正在运行”

iphone - 更改 xib 时 iPhone 上的 SIGABRT

iphone - 如何将运行 iOS 5 的 iPhone 静音

ios - JSQMessagesViewController 中不显示工具栏

iphone - 在 Xcode 中设置 RestKit

iphone - 更改现有 iPhone/iPad 应用程序上的目标设备系列

ios - 如何在应用程序处于非事件状态时获取推送通知 ios swift3

ios - 缺少 NFCTagReaderSession 所需的权利

objective-c - iOS:如何存储要用作多个类中计算的一部分的值(整数)?

iphone - 设备方向将改变而不是已经改变