gpt4 book ai didi

iphone - UIButton 与 UITableViewCell 中的 UIImages - 重叠等

转载 作者:行者123 更新时间:2023-12-03 19:39:03 26 4
gpt4 key购买 nike

我的 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”。

亲切的问候,

多米尼克

最佳答案

在此代码中,您在 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)];

更改高度、宽度和 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];

关于iphone - UIButton 与 UITableViewCell 中的 UIImages - 重叠等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8873003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com