[英]UILabel value does not update in cellForRowAtIndexPath?
每當該視圖出現時,我都會創建自定義單元並更新一些UILabel
。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *lblFacingValue = nil;
static NSUInteger const kFacingLabelTag = 7;
CGFloat LeftLabelWidth = 130.0;
static NSString *CellIdentifier = @"Cell";
ClientState *clientState = [ClientState sharedInstance];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.row == 0)
{
lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblFacingValue.font = [UIFont systemFontOfSize:21];
lblFacingValue.textAlignment = UITextAlignmentLeft;
lblFacingValue.backgroundColor = [UIColor clearColor];
lblFacingValue.tag = kFacingLabelTag;
lblFacingValue.textColor = [UIColor yellowColor];
[cell.contentView addSubview:lblFacingValue];
}
}
lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction];
}
但是問題在於它包含我第一次分配的值,並且在我打開該視圖時不更新它。 該視圖在選項卡中。
它沒有更新lblFacingValue的值並跳過檢查
如果(cell == nil)
第一次之后。 但不要更新lblFacingValue。
Azhar,我認為您忘記了對dequeueReusableCellWithIdentifier的調用未返回nil時,將lblFacingValue變量設置為實際的UILabel對象。 您應該添加:
lblFacingValue = [cell viewWithTag:kFacingLabelTag];
打電話之前
lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction];
希望這可以幫助!
請記住一件事,當單元為零時,您將構造單元。 不要在那里分配值。 只需創建子視圖等。
此代碼應修復它。 另請參閱我的評論。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSUInteger const kFacingLabelTag = 7; //This should rather be a #define ..
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.row == 0)
{
CGFloat LeftLabelWidth = 130.0;
UILabel *lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblFacingValue.font = [UIFont systemFontOfSize:21];
lblFacingValue.textAlignment = UITextAlignmentLeft;
lblFacingValue.backgroundColor = [UIColor clearColor];
lblFacingValue.tag = kFacingLabelTag;
lblFacingValue.textColor = [UIColor yellowColor];
[cell.contentView addSubview:lblFacingValue];
}
}
//now, we might be reusing the cell, so in either case, pick the label
//and edit the value.
ClientState *clientState = [ClientState sharedInstance];
UILabel *theLabel = (UILabel*) [cell.contentView viewForTag:kFacingLabelTag];
theLabel.text = [NSString stringWithFormat:@"%@", clientState.Direction];
}
如果您確實要使一個單元出隊,您會忘記通過標簽獲得標簽的片段。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *lblFacingValue = nil;
static NSUInteger const kFacingLabelTag = 7;
CGFloat LeftLabelWidth = 130.0;
static NSString *CellIdentifier = @"Cell";
ClientState *clientState = [ClientState sharedInstance];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.row == 0)
{
lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblFacingValue.font = [UIFont systemFontOfSize:21];
lblFacingValue.textAlignment = UITextAlignmentLeft;
lblFacingValue.backgroundColor = [UIColor clearColor];
lblFacingValue.tag = kFacingLabelTag;
lblFacingValue.textColor = [UIColor yellowColor];
[cell.contentView addSubview:lblFacingValue];
}
} else {
lblFacingValue = [cell.contentView viewWithTag:kFacingLabelTag];
}
lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction];
}
問題在於您專門配置的單元具有與其他單元相同的單元標識符,
static NSString *CellIdentifier = @"Cell";
因此,在特殊單元格和普通單元格之間使單元出隊時,系統不會區分。 您應該為特殊單元格創建一個不同的單元格標識符,並在indexPath.row == 0時使用該隊列出隊;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.