问题出在我滚动(正如您已经猜到的那样)表格时.图像加载正常,但是当我向上/向下滚动时,图像将位于其他单元格上方,并且当该单元格显示在屏幕上时,图像会迅速将其位置更改为正确的位置,等等.这是我的UITableView的代码: -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{返回1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分{返回[feed count];}-(UITableViewCell *)getCellContentView:(NSString *)cellIdentifier{CGRect rect = [self.tableView界限];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复用标识符: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分配] initWithFrame:PinFrame];imgTemp.tag = 3;[cell.contentView addSubview:imgTemp];UIView * viewTemp = [[UIView分配] initWithFrame:ViewFrame];viewTemp.tag = 4;[viewTemp setBackgroundColor:[UIColor whiteColor]];[cell.contentView addSubview:viewTemp];返回单元}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{静态NSString * CellIdentifier = @"Cell";UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];//UITableViewCell * cell = [self getCellContentView:CellIdentifier];if(cell == nil){cell = [self getCellContentView:CellIdentifier];} 别的 {[cell.contentView viewWithTag:999];}cell.backgroundColor = [UIColor clearColor];UILabel *标题=(UILabel *)[单元格viewWithTag:1];UILabel *旅程=(UILabel *)[单元格viewWithTag:2];UIImageView * pin =(UIImageView *)[cell viewWithTag:3];UILabel *评论=(UILabel *)[单元格viewWithTag:5];title.text = [NSString stringWithFormat:@%@",[[feed feedAtAtIndex: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 feedAtAtIndex:indexPath.row] objectForKey:@"weather_temp"]!= [NSNull null]){trip.text = [NSString stringWithFormat:@这是%@和%@°C.",[[feed feedAtAtIndex:indexPath.row] objectForKey:@"weather_desc"],[[feed feedAtAtIndex:indexPath.row] objectForKey:@"weather_temp"]];} 别的 {trip.text = [NSString stringWithFormat:@%@",[[[feed feedAtAtIndex:indexPath.row] objectForKey:@"journey"] objectForKey:@"name"]];}trip.frame = CGRectMake(journey.frame.origin.x,10 + title.frame.size.height,240,15);如果([[feed feedAtAtIndex:indexPath.row] objectForKey:@"latitude"]!= [NSNull null]){pin.image = [UIImage imageNamed:@"LocationPin.png"];} 别的 {pin.image = [UIImage imageNamed:@"Pin.png"];}if([[feed objectAtIndex:indexPath.row] objectForKey:@"image_file_size"]!= [NSNull null]){NSString * text = [NSString stringWithFormat:@%@",[[feed feedAtAtIndex:indexPath.row] objectForKey:@"comment"]];CGFloat高度= [文本sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(250,1500)lineBreakMode:UILineBreakModeWordWrap] .height;UIImage * myImage = [图像objectForKey:[NSString stringWithFormat:@%i",indexPath.row]];UIButton *按钮= [[UIButton分配] initWithFrame:CGRectZero];[按钮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];[按钮addTarget:self动作:@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)];} 别的 {UIView * backView =(UIView *)[cell viewWithTag:4];[backView setFrame:CGRectZero];[backView setBackgroundColor:[UIColor clearColor]];}comments.text = [NSString stringWithFormat:@%i",[[[feed feedAtAtIndex:indexPath.row] objectForKey:@"comments"] count]];UIImage * background = [UIImage imageNamed:@"CellBackground.png"];UIImageView * imageView = [[UIImageView alloc] initWithImage:background];imageView.contentMode = UIViewContentModeScaleToFill;[cell setBackgroundView:imageView];返回单元}-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath{[self.tableView deselectRowAtIndexPath:indexPath animation:NO];MilestoneView * MilestoneView = [[[MilestoneView分配] initWithNibName:@"MilestoneView" bundle:nil];[milestoneView setMilestone:[feed objectAtIndex:indexPath.row]];[self.navigationController pushViewController:milestoneView动画:是];}-(CGFloat)tableView:(UITableView *)电视heightForRowAtIndexPath:(NSIndexPath *)indexPath{NSString * text = [NSString stringWithFormat:@%@",[[feed feedAtAtIndex:indexPath.row] objectForKey:@"comment"]];CGFloat高度= [文本sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(240,1500)lineBreakMode:UILineBreakModeWordWrap] .height;if([[feed objectAtIndex:indexPath.row] objectForKey:@"image_file_size"]!= [NSNull null]){UIImage * myImage = [图像objectForKey:[NSString stringWithFormat:@%i",indexPath.row]];高度+ = myImage.size.height + 15.0f;}返回高度+ 37;} 我还在iOS 5.0上使用Apple内置的"ARC".亲切的问候,多米尼克解决方案最终通过执行以下简单代码来解决该问题: NSIndexPath * indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]]; 我使用UIButton发送器来获取它所在的单元格.我还撤出了.tag的所有修改,并将它们保留为相同的数字.I have a problem with my UITableViewCell's. I am developing an application where certain posts in a feed may contain an image, and the button clicked will also need to contain a 'tag' number depending on which row of the table it is as I have an array of images downloaded.The problem lies when I am scrolling (as you have already guessed) the table. The image loads fine, however when I scroll up/down the image will then go above other cells, and when the cell is shown back on the screen, the image will quickly change it's position to the proper position etc.Here's the code for my 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;}I am also using Apple's inbuilt 'ARC' with iOS 5.0.Kind regards,Dominic 解决方案 Ended up fixing the solution by doing the following simple code:NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]];I used the UIButton sender to get the cell that it was within. I also pulled out all the modifications of the .tag's and kept them at the same number. 这篇关于UITableViewCell中带有UIImages的UIButton-重叠等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 21:28