【问题标题】:Show button in Customized table view ios在自定义表格视图 ios 中显示按钮
【发布时间】:2013-05-31 11:05:39
【问题描述】:

我是新人,我添加了这段代码来自定义表格视图,其中包含图像和按钮

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"ImageOnRightCell";
    UILabel *mainLabel, *secondLabel;
    UIImageView *photo;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil)
    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 0.0, 100, 20)];
        [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:1];
        [cell.contentView addSubview:button];

        photo = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 0.0, 80.0, 45.0)];
        photo.tag = PHOTO_TAG;


        photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;

        [cell.contentView addSubview:photo];
   }

    UIImage *theImage = [UIImage imageNamed:@"man.jpg"];
    photo.image = theImage;

    return cell;

}

图像部分出现在表格的行中,但不是按钮。 请问这是什么问题

【问题讨论】:

  • 请修正代码格式。
  • 尝试给按钮标题或背景颜色。实际上自定义单元格是不错的选择
  • 问题出在按钮的框架上。部分按钮位于 imageView 后面。将按钮的 x 坐标更改为 150 并查看

标签: ios uitableview customization


【解决方案1】:

试试看……

UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeCustom];
finalPriceBtn.tag=i+200;
finalPriceBtn.frame = CGRectMake(100, 0.0, 100, 20);
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
[finalPriceBtn setTitleColor:[UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1] forState:UIControlStateSelected];
[finalPriceBtn setTitleColor:[UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[cell.contentView addSubview:finalPriceBtn];

希望我能帮上忙。

【讨论】:

  • 我如何只选择按钮单击而不是表格视图的单元格
  • @user2256034 : -(IBAction)goBtnClk:(id)sender { int i = [sender tag];我 = 我 - 200; // 选择按钮id }
  • @user2256034:发生了什么?
【解决方案2】:

您正在添加按钮,然后添加图像,因此图像位于按钮顶部,请执行以下操作:

 [cell.contentView addSubview:photo];

在此之前:

 [cell.contentView addSubview:button];

【讨论】:

    【解决方案3】:

    您正在按钮上方添加图像,因此请将其添加到按钮下方。

    像这样使用你的代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"ImageOnRightCell";
        UILabel *mainLabel, *secondLabel;
        UIImageView *photo;
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
            photo = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 0.0, 80.0, 45.0)];
            photo.tag = PHOTO_TAG;
            photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
            [cell.contentView addSubview:photo];
    
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(100, 0.0, 100, 20);
            [button setBackgroundColor:[UIColor redColor]];// Here add some background color to check its visibility
            [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:button];
       }
    
       UIImage *theImage = [UIImage imageNamed:@"man.jpg"];
       photo.image = theImage;
    
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-21
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多