UITableViewCell 中带有 UIImages 的 UIButton - 重叠等

Posted

技术标签:

【中文标题】UITableViewCell 中带有 UIImages 的 UIButton - 重叠等【英文标题】:UIButton with UIImages in a UITableViewCell - Overlapping etc 【发布时间】:2012-01-15 20:15:41 【问题描述】:

我的 UITableViewCell 有问题。我正在开发一个应用程序,其中提要中的某些帖子可能包含图像,并且单击的按钮还需要包含一个“标签”编号,具体取决于表格的哪一行,因为我下载了一组图像。

问题出在我滚动表格时(正如您已经猜到的)。图像加载正常,但是当我向上/向下滚动时,图像会出现在其他单元格上方,当单元格重新显示在屏幕上时,图像会迅速将其位置更改为正确的位置等。

这是我的 UITableView 的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return [feed count];


- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier 

    CGRect rect = [self.tableView bounds];
    CGRect CellFrame = CGRectMake(0.0f, 0.0f, rect.size.width, 10.0f);
    CGRect TitleFrame = CGRectMake(55.0f, 10.0f, 240.0f, 15.0f);
    CGRect TextFrame = CGRectMake(55.0f, 25.0f, 250.0f, 15.0f);
    CGRect PinFrame = CGRectMake(21.0f, 10.0f, 30.0f, 30.0f);
    CGRect ViewFrame = CGRectMake(50.0f, 35.0f, 260.0f, 0.0f);
    CGRect CommentsFrame = CGRectMake(300.0f, 10.0f, 20.0f, 15.0f);
    UILabel * lblTemp;

    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    [cell setFrame:CellFrame];

    lblTemp = [[UILabel alloc] initWithFrame:TitleFrame];
    lblTemp.tag = 1;
    lblTemp.numberOfLines = 0;
    lblTemp.opaque = NO;
    lblTemp.font = [UIFont systemFontOfSize:13.0f];
    lblTemp.backgroundColor = [UIColor clearColor];
    lblTemp.textColor = [UIColor blackColor];
    lblTemp.textAlignment = UITextAlignmentLeft;
    lblTemp.shadowColor = [UIColor whiteColor];
    lblTemp.shadowOffset = CGSizeMake(0, 1);
    [cell.contentView addSubview:lblTemp];

    lblTemp = [[UILabel alloc] initWithFrame:TextFrame];
    lblTemp.tag = 2;
    lblTemp.numberOfLines = 0;
    lblTemp.lineBreakMode = UILineBreakModeWordWrap;
    lblTemp.adjustsFontSizeToFitWidth = NO;
    lblTemp.font = [UIFont systemFontOfSize:13.0f];
    lblTemp.textColor = [UIColor lightGrayColor];
    lblTemp.backgroundColor = [UIColor clearColor];
    lblTemp.shadowColor = [UIColor whiteColor]; 
    lblTemp.shadowOffset = CGSizeMake(0, 1);
    [cell.contentView addSubview:lblTemp];

    lblTemp = [[UILabel alloc] initWithFrame:CommentsFrame];
    lblTemp.tag = 5;
    lblTemp.numberOfLines = 1;
    lblTemp.font = [UIFont systemFontOfSize:13.0f];
    lblTemp.textColor = [UIColor lightGrayColor];
    lblTemp.backgroundColor = [UIColor whiteColor];
    lblTemp.shadowColor = [UIColor whiteColor];
    lblTemp.shadowOffset = CGSizeMake(0, 1);
    [cell.contentView addSubview:lblTemp];

    UIImageView * imgTemp = [[UIImageView alloc] initWithFrame:PinFrame];
    imgTemp.tag = 3;
    [cell.contentView addSubview:imgTemp];

    UIView * viewTemp = [[UIView alloc] initWithFrame:ViewFrame];
    viewTemp.tag = 4;
    [viewTemp setBackgroundColor:[UIColor whiteColor]];
    [cell.contentView addSubview:viewTemp];

    return cell;


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

    static NSString * CellIdentifier = @"Cell";

    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //UITableViewCell * cell = [self getCellContentView:CellIdentifier];

    if (cell == nil) 
        cell = [self getCellContentView:CellIdentifier];
     else 
        [cell.contentView viewWithTag:999];
    

    cell.backgroundColor = [UIColor clearColor];

    UILabel * title         = (UILabel *)[cell viewWithTag:1];
    UILabel * journey       = (UILabel *)[cell viewWithTag:2];
    UIImageView * pin       = (UIImageView *) [cell viewWithTag:3];
    UILabel * comments      = (UILabel *)[cell viewWithTag:5];

    title.text = [NSString stringWithFormat:@"%@", [[feed objectAtIndex:indexPath.row] objectForKey:@"comment"]];
    [title sizeToFit];
    title.frame = CGRectMake(title.frame.origin.x, title.frame.origin.y, 240, title.frame.size.height);

    if ([[feed objectAtIndex:indexPath.row] objectForKey:@"weather_desc"] != [NSNull null] ||
        [[feed objectAtIndex:indexPath.row] objectForKey:@"weather_temp"] != [NSNull null]) 
            journey.text = [NSString stringWithFormat:@"It's %@ and %@°C.", [[feed objectAtIndex:indexPath.row] objectForKey:@"weather_desc"], [[feed objectAtIndex:indexPath.row] objectForKey:@"weather_temp"]];
     else 
        journey.text = [NSString stringWithFormat:@"%@", [[[feed objectAtIndex:indexPath.row] objectForKey:@"journey"] objectForKey:@"name"]];
    
    journey.frame = CGRectMake(journey.frame.origin.x, 10 + title.frame.size.height, 240, 15);

    if ([[feed objectAtIndex:indexPath.row] objectForKey:@"latitude"] != [NSNull null]) 
        pin.image = [UIImage imageNamed:@"LocationPin.png"];
     else 
        pin.image = [UIImage imageNamed:@"Pin.png"];
    

    if ([[feed objectAtIndex:indexPath.row] objectForKey:@"image_file_size"] != [NSNull null]) 
        NSString * text = [NSString stringWithFormat:@"%@", [[feed objectAtIndex:indexPath.row] objectForKey:@"comment"]];
        CGFloat height = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(250, 1500) lineBreakMode:UILineBreakModeWordWrap].height;

        UIImage * myImage = [images objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]];

        UIButton * button = [[UIButton alloc] initWithFrame:CGRectZero];
        [button setBackgroundImage:myImage forState:UIControlStateNormal];
        [button setFrame:CGRectMake(title.frame.origin.x + 55.0f, 40.0f + height, 250.0f, myImage.size.height)];
        [button setTag:10 + indexPath.row];
        [button addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button];

        UIView * backView = (UIView *) [cell viewWithTag:4];
        [backView setFrame:CGRectMake(title.frame.origin.x + 50.0f, 35.0f + height, 260.0f, myImage.size.height + 10.0f)];
     else 
        UIView * backView = (UIView *) [cell viewWithTag:4];
        [backView setFrame:CGRectZero];
        [backView setBackgroundColor:[UIColor clearColor]];
    

    comments.text = [NSString stringWithFormat:@"%i", [[[feed objectAtIndex:indexPath.row] objectForKey:@"comments"] count]];

    UIImage * background = [UIImage imageNamed:@"CellBackground.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:background];
    imageView.contentMode = UIViewContentModeScaleToFill;
    [cell setBackgroundView:imageView];

    return cell;


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

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    MilestoneView * milestoneView = [[MilestoneView alloc] initWithNibName:@"MilestoneView" bundle:nil];
    [milestoneView setMilestone:[feed objectAtIndex:indexPath.row]];
    [self.navigationController pushViewController:milestoneView animated:YES];


- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath 

    NSString * text = [NSString stringWithFormat:@"%@", [[feed objectAtIndex:indexPath.row] objectForKey:@"comment"]];
    CGFloat height = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(240, 1500) lineBreakMode:UILineBreakModeWordWrap].height;

    if ([[feed objectAtIndex:indexPath.row] objectForKey:@"image_file_size"] != [NSNull null]) 
        UIImage * myImage = [images objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]];
        height += myImage.size.height + 15.0f;
    

    return height + 37;

我还在 ios 5.0 中使用 Apple 的内置“ARC”。

亲切的问候,

多米尼克

【问题讨论】:

老实说,您应该只使用 NIB 并完成它。代码太多,无法有效管理和维护。 【参考方案1】:

在此代码中,您在 cellForRowAtIndexPath 中添加按钮,因此会发生这种情况.... 因此,您在 getCellContentView 中添加 button 、 backView 的代码,并从 getCellContentView 中的数组中获取图像。

UIImage *myImage = [images objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]];

那么,

UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
[button setBackgroundImage:myImage forState:UIControlStateNormal];
[button setFrame:CGRectMake(55.0f, 40.0f, 250.0f, myImage.size.height)];

改变 height , width & x,y co-or。你想要什么 将单元格索引设置为按钮。

[button setTag:indexPath.row];
[button addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];

如果您有任何问题,请告诉我......

替换下面的函数头

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier 
.
.

有这个功能

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier  indexPathForButtonTag:(NSIndexPath*)indexPath 
.
.

替换 cellForRowAtIndex 中的以下行

cell = [self getCellContentView:CellIdentifier];

随着

cell = [self getCellContentView:CellIdentifier indexPathForButtonTag:indexPath];

【讨论】:

唯一的问题是按钮标签会根据它所在的行而变化。【参考方案2】:

您不需要每个按钮都有不同的标签。您不应该向 cellForRowAtIndexPath 中的单元格添加任何子视图,因为您最终将不可避免地在子视图之上添加子视图。我注意到您还在此方法中添加了图像视图作为单元格的背景 - 您应该只在创建新单元格时执行此操作。

我假设您没有使用 xib 文件进行此单元格布局是有充分理由的,因此只需在您的 contentView 方法中添加按钮并为其指定一个已知标记。

在按钮的操作方法中,您可以使用locationInView:indexPathForRowAtPoint: 方法找出按钮在表格的哪一行:

CGPoint hit = [sender locationInView:self.tableView];
NSIndexPath *hitRow = [self.tableView indexPathForRowAtPoint:hit];

另外,请注意将视图的 .hidden 属性设置为 YES 可能比将其框架设置为 CGRectZero 更省力。

【讨论】:

【参考方案3】:

通过执行以下简单代码最终修复了解决方案:

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]];

我使用 UIButton 发送器来获取它所在的单元格。我还删除了 .tag 的所有修改,并将它们保持在相同的编号。

【讨论】:

以上是关于UITableViewCell 中带有 UIImages 的 UIButton - 重叠等的主要内容,如果未能解决你的问题,请参考以下文章

Swift中带有按钮的UITableViewCell [重复]

UITableViewCell 中带有 UIImages 的 UIButton - 重叠等

UITableViewCell 中带有多行标签的 UIStackView 高度不正确

在 xib 中带有可选 UIImageView 的自定义 UITableViewCell

UITableViewCell 中带有约束的动画启动不正确

UIViewControllers 中带有许多 UITableVIew 的 UIScrollView 在点击时消失 UITableViewCell