【问题标题】:add uiview at row selection在行选择处添加 uiview
【发布时间】:2012-02-27 07:39:17
【问题描述】:

我想当包含两个按钮的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 没有出现。如何解决?

【问题讨论】:

    标签: iphone ios ipad uitableview uiview


    【解决方案1】:

    尝试:

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

    【讨论】:

    • 我已经编辑了答案,我已经测试过了,它工作正常!
    【解决方案2】:
    UIView *v = [[UIView alloc] init];//You have not set the frame of the v;
    

    【讨论】:

      【解决方案3】:

      initWithFrame 是视图的指定初始化器。

      由于您使用的是init,它不知道要创建多大的视图。

      另外UIButton *button1 = [[UIButton alloc] init]; 也不正确。 UIButtons 是使用buttonWithType: 类方法创建的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-23
        • 1970-01-01
        • 2021-03-27
        • 2021-07-27
        相关资源
        最近更新 更多