gpt4 book ai didi

iOS如何有效地将UIView添加到tableview单元格

转载 作者:行者123 更新时间:2023-11-29 04:24:12 25 4
gpt4 key购买 nike

我有一个表格,其中每个单元格都有一个由 XMLParser feed 提供的 UIView。如果我保持原样,每次刷新 View 时, View 都会相互构建,因为它们没有被释放/删除,这是完全有道理的。我将如何编写代码来避免这个问题,而不必每次刷新时都重建 View 。我被它的逻辑和实际语法所困扰。谢谢。

代码:

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

static NSString *CellIdentifier = @"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture.png"]];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 200, 65)];
[selectionView setBackgroundColor: [UIColor clearColor]];
cell.selectedBackgroundView = selectionView;
}

// Display content in each cell

cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 300, 65)];
[cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[cellSeparator setContentMode:UIViewContentModeTopLeft];
[cellSeparator setBackgroundColor: [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0]];
cellSeparator.layer.borderWidth = 1.0;
cellSeparator.layer.borderColor = [UIColor blackColor].CGColor;
[cell addSubview:cellSeparator];

callTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 190, 21)];
callTypeLabel.text = [currentCall currentCallType];
callTypeLabel.backgroundColor = [UIColor clearColor];
callTypeLabel.font = [UIFont boldSystemFontOfSize:12.0];
[cellSeparator addSubview:callTypeLabel];

locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 17 , 190, 15)];
locationLabel.text = [currentCall location];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.font = [UIFont systemFontOfSize:10.0];
[cellSeparator addSubview:locationLabel];

unitsLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 43, 190, 21)];
unitsLabel.text = [currentCall units];
unitsLabel.backgroundColor = [UIColor clearColor];
unitsLabel.font = [UIFont italicSystemFontOfSize:10.0];
[cellSeparator addSubview:unitsLabel];

stationLabel = [[UILabel alloc]initWithFrame:CGRectMake(195 , 25, 75, 20)];
NSString *station = [@"Station: " stringByAppendingString:currentCall.station];
stationLabel.text = station;
stationLabel.backgroundColor = [UIColor clearColor];
stationLabel.font = [UIFont boldSystemFontOfSize:11.0];
[cellSeparator addSubview:stationLabel];

if ([currentCall.county isEqualToString:@"W"]) {
UIImage *countyImage = [UIImage imageNamed:@"blue.png"];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.image = countyImage;
[cellSeparator addSubview:countyImageLabel];
} else {
UIImage *countyImage = [UIImage imageNamed:@"green.png"];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.image = countyImage;
[cellSeparator addSubview:countyImageLabel];
}

if ([currentCall.callType isEqualToString:@"F"]) {
UIImage *callTypeImage = [UIImage imageNamed:@"red.png"];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.image = callTypeImage;
[cellSeparator addSubview:callTypeImageLabel];
} else {
UIImage *callTypeImage = [UIImage imageNamed:@"yellow.png"];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.image = callTypeImage;
[cellSeparator addSubview:callTypeImageLabel];
}

return cell;
}

最佳答案

如果我正确理解你的问题,我认为你想要的解决方案是子类化 UITableViewCell,设置你的 subview (UIImageViews 等)并使其属性你的子类。然后你就可以

if (!cell.imageView) {
cell.imageView = [[UIImageView alloc] initWithFrame:myFrame];
}

然后只需设置图像即可。这样,您就可以确保不会将 ImageView 堆叠在一起,并且当单元格出队时,将设置正确的图像。

编辑:

创建一个名为 MyTableViewCell 的类(或者任何你想要的名称,但要确保它是 UITableViewCell 的子类)添加@property(强,非原子)UIImageView *imageView;

然后在 cellForRowAtIndexPath 中使用 MyTableViewCell 而不是 UITableViewCell,您可以访问自定义 TableView 单元格的图像属性,检查它是否存在,如果不存在则创建它(这就是上面代码的作用),然后设置图像。我会给你密码,但我在打电话。 Google 子类化 UITableviewCell。您应该找到大量示例。

关于iOS如何有效地将UIView添加到tableview单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593440/

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