gpt4 book ai didi

objective-c - iOS:如何正确访问 UITableViewCell 的 contentView 上的自定义标签?

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

我有一个 UITableView,每个单元格被分成几个部分,每个部分都有不同的标签。该表由 NSDictionaries 的 NSArray 填充,其中包含填充单元格标签的所有数据。 UITableView 的这一部分效果很好。

当我更改其中一个 NSDictionaries 中的一些值,然后使用更新的 NSArray 重新加载表时,就会出现问题。通常,当我调用 [myTableView reloadData];即使(通过调试)我可以看到正在处理的更新数据,也没有任何更新。但是如果我改变标准:if (cell == nil) {if (1) { ,在 cellForRowAtIndexPath方法,然后它工作得很好。我明白为什么 if(1) {有效,但我不明白为什么我不能重复使用单元格而只是更改标签文本。

为什么if (cell == nil)不行?重新初始化每个单元格是否会消耗大量资源?

代码:

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

static NSString *cellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];

if (/*cell == nil*/1) {
// Initialize Custom Cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

//// Background View
cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)];
[cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Images/Library/phase_table_cell.png"]]];

//// Name Label
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 18)];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[cellBackgroundView addSubview:nameLabel];

//// Percent Complete Label
percentCompleteLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 10, 30, 18)];
[percentCompleteLabel setBackgroundColor:[UIColor clearColor]];
[percentCompleteLabel setTextAlignment:UITextAlignmentCenter];
[cellBackgroundView addSubview:percentCompleteLabel];

//// Overall Status Label
overallStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(352, 7, 63, 30)];
[overallStatusLabel setBackgroundColor:[UIColor clearColor]];
[overallStatusLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
[overallStatusLabel setLineBreakMode:UILineBreakModeWordWrap];
[overallStatusLabel setNumberOfLines:2];
[overallStatusLabel setTextAlignment:UITextAlignmentCenter];
[cellBackgroundView addSubview:overallStatusLabel];

//// Finish Date Label
finishDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(425, 10, 55, 18)];
[finishDateLabel setBackgroundColor:[UIColor clearColor]];
[finishDateLabel setTextAlignment:UITextAlignmentCenter];
[finishDateLabel setFont:[UIFont systemFontOfSize:12.0]];
[cellBackgroundView addSubview:finishDateLabel];

//// Overall Weight Label
overallWeightLabel = [[UILabel alloc] initWithFrame:CGRectMake(505, 10, 30, 18)];
[overallWeightLabel setBackgroundColor:[UIColor clearColor]];
[overallWeightLabel setTextAlignment:UITextAlignmentCenter];
[cellBackgroundView addSubview:overallWeightLabel];

//// Green Risk View
greenRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 4, 61, 10)];
[greenRiskView setBackgroundColor:[UIColor greenColor]];
[greenRiskView setHidden:YES];
[cellBackgroundView addSubview:greenRiskView];

//// Yellow Risk View
yellowRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 17, 61, 10)];
[yellowRiskView setBackgroundColor:[UIColor yellowColor]];
[yellowRiskView setHidden:YES];
[cellBackgroundView addSubview:yellowRiskView];

//// Red Risk View
redRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 30, 61, 10)];
[redRiskView setBackgroundColor:[UIColor redColor]];
[redRiskView setHidden:YES];
[cellBackgroundView addSubview:redRiskView];

[cell.contentView addSubview:cellBackgroundView];
}

// Get Current Dictionary
NSDictionary *dictForIndexPath = [self.phaseArray objectAtIndex:[indexPath row]];
// Set Elements
[nameLabel setText:[dictForIndexPath objectForKey:@"name"]];
[percentCompleteLabel setText:[dictForIndexPath objectForKey:@"percentComplete"]];
[overallStatusLabel setText:[dictForIndexPath objectForKey:@"overallStatus"]];
[overallWeightLabel setText:[[NSNumber numberWithInt:[[dictForIndexPath objectForKey:@"overallWeight"] intValue]] stringValue]];
//// Create Finish Date String
NSString *finishDateString = [NSString stringWithFormat:@"%@/%@/%@", [dictForIndexPath objectForKey:@"finishDay"], [dictForIndexPath objectForKey:@"finishMonth"], [dictForIndexPath objectForKey:@"finishYear"]];
[finishDateLabel setText:finishDateString];
//// Pick Risk View
NSString *riskColor = [dictForIndexPath objectForKey:@"riskColor"];
if ([riskColor isEqualToString:@"Green"]) {
[greenRiskView setHidden:NO];
[yellowRiskView setHidden:YES];
[redRiskView setHidden:YES];
} else if ([riskColor isEqualToString:@"Yellow"]) {
[greenRiskView setHidden:YES];
[yellowRiskView setHidden:NO];
[redRiskView setHidden:YES];
} else {
[greenRiskView setHidden:YES];
[yellowRiskView setHidden:YES];
[redRiskView setHidden:NO];
}

return cell;
}

最佳答案

可能您正在分配 if(cell==nil) 中的值堵塞?只有初始化应该发生在该 block 内。将其余部分移出。 (发布您的完整 cellForRow 代码,如果您需要更多帮助)

//编辑:现在,在您发布代码后,我看到了您的问题:

您将所有标签和 View 存储在成员变量中.. 当然,只有在 if(cell != nil) 时才会发生这种情况。 block 被执行。之后,您总是访问同一个单元格(最后分配的)。所以你至少要更新一个单元格;)

要解决您的问题,请工作,例如使用标签从单元格中获取相应的 View 。我将为您的 backgroundView 显示它,但您必须为所有 View 执行此操作(成员变量的 INSTEAD。删除它们。)

static NSString *cellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];

if (cell == nil) {
//// Background View
UIView* cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)];
cellBackgroundView.tag = 1;
[cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Images/Library/phase_table_cell.png"]]];
}

// get the backgroundview from the current cell
UIView* backgroundView = [cell.contentView viewWithTag: 1];

等等..

关于objective-c - iOS:如何正确访问 UITableViewCell 的 contentView 上的自定义标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9864140/

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