[英]UILabel Text value inside UITableViewCell Swaps
每次UILabel
UITableViewCell
UILabel
Text值都會被交換。因為我的cell height
為140 cell.TextLabel.text
無法設置frame
並使之顯示在中間,但是我需要在單元格的開頭顯示TitleText .so,我使用UILabel
來按需顯示。但是每次加載單元格時,它們為那些單元格顯示錯誤的標題。但是當我記錄該數組時,它會正確記錄日志,並且如果我使用cell.TextLabel.text
檢查它cell.TextLabel.text
正確顯示。但在UILabel
沒有。混亂的很多。無法追蹤我哪里出了錯。 以下是我的代碼。
`
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 140;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *Title;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Title = [[UILabel alloc]init ];
Title.frame =CGRectMake(20, 18, 152, 23);
[Title sizeToFit];
}
NSMutableArray *contentArray = [sectionDict valueForKey:[array objectAtIndex:indexPath.section]];
NSLog(@"content %@",contentArray);
Title.text = [contentArray objectAtIndex:indexPath.row];
[cell addSubview:Title];
UIImageView *seaImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PacificOcean.png" ]];
seaImage.frame = CGRectMake(20, 60, 280, 80);
[cell addSubview:seaImage];
return cell;
}
`
將您的子視圖標簽和圖像添加到每個子視圖的cell == nil
block set標簽的單元格中。 通過標記訪問您的子視圖。 希望對您有幫助。
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *Title = [[UILabel alloc]init ];
Title.tag = 1000;
Title.frame =CGRectMake(20, 18, 152, 23);
[Title sizeToFit];
[cell.contentView addSubview:Title];
UIImageView *seaImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PacificOcean.png" ]];
seaImage.frame = CGRectMake(20, 60, 280, 80);
[cell.contentView addSubview:seaImage];
}
NSMutableArray *contentArray = [sectionDict valueForKey:[array objectAtIndex:indexPath.section]];
UILabel *Title = (UILabel *)[cell.contentView viewWithTag:1000];
Title.text = [contentArray objectAtIndex:indexPath.row];
return cell;
像這樣更改您的代碼。
像這樣使用您的代碼
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *Title;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Title = [[UILabel alloc]init ];
Title.frame =CGRectMake(20, 18, 152, 23);
[Title sizeToFit];
[cell.contentView addSubview: Title];
UIImageView *seaImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PacificOcean.png" ]];
seaImage.frame = CGRectMake(20, 60, 280, 80);
[cell.contentView addSubview: seaImage];
}
NSMutableArray *contentArray = [sectionDict valueForKey:[array objectAtIndex:indexPath.section]];
NSLog(@"content %@",contentArray);
Title.text = [contentArray objectAtIndex:indexPath.row];
return cell;
}
我不知道這是否是您的錯字,但是cell.TextLabel.text
是NSString,因此您不能將frame設置為那個,但是cell.TextLabel.frame
應該可以工作。 您使用的方法中的另一個問題是UILabel *Title;
在if語句外部聲明,然后在if語句結束后繼續執行,如果成功出隊單元格將為nil
。 而且由於Objective-C對您如此友好,因此當您將消息傳遞給nil
時也不會抱怨。 如果要使用此批准,則還需要在if語句中使用標簽將標簽添加到單元格中,然后在設置框架時獲得相同的標簽。 這與seaImage
相同,您不應在每次提供一個單元時都添加一個新的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.