2012-01-15 41 views
3

我的UITableViewCell存在问题。我正在开发一个应用程序,其中某个Feed中的某些帖子可能包含一个图片,并且点击的按钮还需要包含一个“标签”号码,具体取决于表格的哪一行,因为我已下载一组图片。在UITableViewCell中使用UIImage的UIButton - 重叠等

问题在于当我滚动(如你已经猜到的)表格时。图像加载正常,但是当我向上/向下滚动时,图像将会在其他单元格上方移动,并且当单元格显示回屏幕上时,图像将快速将其位置改变到正确的位置等。

这里是我的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'。

亲切的问候,

多米尼克

+0

说实话,你应该只使用一个NIB并做好它。这是太多的代码来有效地管理和维护。 – 2012-01-17 23:34:12

回答

0

端固定起来通过执行以下操作简单的代码的解决方案:

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

我用UIButton的发送者获取,这是内部的细胞。我还抽出了.tag的所有修改,并将它们保留在相同的编号。

4

在这段代码中你的cellForRowAtIndexPath添加按钮,因此这是发生....
所以,你添加代码按钮,backView在getCellContentView并获取图像来自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)];

变化高度,宽度&的x,y坐标或。你想要什么和 设置单元格索引到按钮。

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

唯一的问题是按钮标签的改变取决于它所在的行。 – Domness 2012-01-18 23:47:50

1

你不需要每个按钮有不同的标签。您不应该在cellForRowAtIndexPath中的单元格中添加任何子视图,因为您不可避免地最终会在子视图顶部添加子视图。我注意到你也在这个方法中添加了一个图像视图作为单元格的背景 - 当你创建一个新的单元格时,你只应该这样做。

我会假设你没有为这个单元格布局使用xib文件,所以只需在contentView方法中添加按钮并给它一个已知标签。

在您的按钮的操作方法,你可以找出表的按钮是通过使用locationInView:indexPathForRowAtPoint:方法哪一行:

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

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