1

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

  - (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;

}

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

4

3 回答 3

2

试试看....

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

希望我有所帮助。

于 2013-05-31T11:14:46.653 回答
1

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

 [cell.contentView addSubview:photo];

在这之前:

 [cell.contentView addSubview:button];
于 2013-05-31T11:14:40.777 回答
1

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

像这样使用您的代码:

- (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;
}
于 2013-05-31T11:14:52.903 回答