我的UITableViewCell存在问题。我正在开发一个应用程序,其中一个Feed中的某些帖子可能包含一个图像,并且点击的按钮也需要包含一个“标签”编号,具体取决于该表的哪一行,因为我已经下载了一系列图像。
当我滚动(正如你已经猜到的)桌子时,问题出现了。图像加载很好,但是当我向上/向下滚动时,图像将会移到其他单元格上方,当单元格显示在屏幕上时,图像会快速将其位置更改为正确的位置等。
这是我的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;
}
我也在使用Apple内置的'ARC'和iOS 5.0。
亲切的问候,
星
答案 0 :(得分:4)
在此代码中,您在cellForRowAtIndexPath中添加按钮,因此会发生这种情况.... 所以,你在getCellContentView中添加了按钮,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)];
改变高度,宽度和宽度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];
答案 1 :(得分:1)
您不需要每个按钮都有不同的标签。您不应该在cellForRowAtIndexPath中向单元格添加任何子视图,因为您将不可避免地最终在子视图上添加子视图。我注意到您还在此方法中添加了一个图像视图作为单元格的背景 - 您只应在创建新单元格时执行此操作。
我将假设您没有使用xib文件进行此单元格布局,因此只需在contentView方法中添加该按钮并为其指定已知标记即可。
在按钮的操作方法中,您可以使用locationInView:
和indexPathForRowAtPoint:
方法找出按钮所在表格的哪一行:
CGPoint hit = [sender locationInView:self.tableView];
NSIndexPath *hitRow = [self.tableView indexPathForRowAtPoint:hit];
另外,请注意,将视图的.hidden
属性设置为YES可能会减少将其框架设置为CGRectZero的工作量。
答案 2 :(得分:0)
通过执行以下简单代码结束修复解决方案:
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]];
我使用UIButton发件人来获取它所在的单元格。我还提取了.tag的所有修改并将它们保持在相同的数字。