gpt4 book ai didi

ios - 带有 subview 的 UITableViewCell 无法正常工作

转载 作者:行者123 更新时间:2023-12-01 18:14:42 25 4
gpt4 key购买 nike

我的 iOS 应用程序中有 UITableView,我想向单元格添加一些 subview 。我通过使用来做到这一点
[cell.contentView addSubview:someView];
它运作良好,但是......当我向下滚动时, subview 开始隐藏在顶部的单元格中,当我滚动回顶部时,它们不会再次出现......我做错了什么?请问有什么解决办法吗?

编辑

主要是,我在谈论“detailtext”标签,但在更多情况下我会遇到这些问题......

这是完整的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell;

switch (indexPath.row) {
case 0:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
break;

default:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
break;
}


UIView *separatorLine = [[UIView alloc] init];

separatorLine.frame = CGRectMake(15.0f, 60 - 0.5f, cell.frame.size.width-15.0f, 0.5f);
separatorLine.tag = 4;
separatorLine.backgroundColor = [UIColor lightGrayColor];

cell.layer.masksToBounds = NO;

tableView.backgroundColor = [UIColor colorWithRed:33.0 / 255.0 green:157.0 / 255.0 blue:147.0 / 255.0 alpha:1.0];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

UIView *row2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];

UIView *profileBorder = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-50, 50, 102, 102)];

profileBorder.layer.borderColor = [UIColor whiteColor].CGColor;
profileBorder.layer.borderWidth = 5; //2
profileBorder.layer.cornerRadius = 50;


NZCircularImageView *profileImage = [[NZCircularImageView alloc] initWithFrame:CGRectMake(1,1, 100, 100)];
profileImage.image = profilePhoto;
profileImage.contentMode = UIViewContentModeScaleAspectFill;

UITapGestureRecognizer *showBigProfilePhoto = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];

profileImage.userInteractionEnabled = YES;
[profileImage addGestureRecognizer:showBigProfilePhoto];

[profileBorder addSubview:profileImage];

UILabel *numberFeelings = [[UILabel alloc] initWithFrame:CGRectMake(10, 100-25, 100, 50)];

numberFeelings.text = [NSString stringWithFormat:@"%@\nFeelings", feelings];
numberFeelings.font = [UIFont boldSystemFontOfSize:16];
numberFeelings.textAlignment = NSTextAlignmentCenter;
numberFeelings.textColor = [UIColor whiteColor];
numberFeelings.numberOfLines = 0;

UILabel *numberFriends = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2+60, 100-25, 100, 50)];

numberFriends.text = [NSString stringWithFormat:@"%@\nFollowers", friends];
numberFriends.font = [UIFont boldSystemFontOfSize:16];
numberFriends.textColor = [UIColor whiteColor];
numberFriends.numberOfLines = 0;
numberFriends.textAlignment = NSTextAlignmentCenter;

[row2 addSubview:profileBorder];
[row2 addSubview:numberFriends];
[row2 addSubview:numberFeelings];

int rectButtons = cell.frame.size.width-246;

UIImageView *graph = [[UIImageView alloc] initWithFrame:CGRectMake(rectButtons/2, -20, 82, 82)];
UIImageView *badgets = [[UIImageView alloc] initWithFrame:CGRectMake(rectButtons/2+82, -20, 82, 82)];
UIImageView *photos = [[UIImageView alloc] initWithFrame:CGRectMake(rectButtons/2+164, -20, 82, 82)];

graph.image = [UIImage imageNamed:@"graph.jpg"];
badgets.image = [UIImage imageNamed:@"badgets.jpg"];
photos.image = [UIImage imageNamed:@"photos.jpg"];

graph.userInteractionEnabled = YES;
badgets.userInteractionEnabled = YES;
photos.userInteractionEnabled = YES;

UITapGestureRecognizer *graphTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showGraph:)];

[graph addGestureRecognizer:graphTap];

NSArray *jmenoCasti = [name componentsSeparatedByString:@" "];

krestni = [jmenoCasti objectAtIndex:0];

int indexOfPost = indexPath.row-3;

NSMutableAttributedString *str;
int countFeeling;
int countString;
int countBeforeFeeling;

if (indexPath.row >=3) {

str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ was %@", krestni, [naladyHim objectAtIndex:[[[posts objectAtIndex:indexOfPost] objectForKey:@"_feel"] integerValue]]]];

countFeeling = [[naladyHim objectAtIndex:[[[posts objectAtIndex:indexOfPost] objectForKey:@"_feel"] integerValue]] length];

countString = krestni.length+5+countFeeling;

countBeforeFeeling = countString-countFeeling+1;

int rangeStart = countBeforeFeeling-1;
int rangeStop = str.length-rangeStart;

NSLog(@"%i ... %i", countBeforeFeeling-1, countString-1);

[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica-Bold" size:16.0f] range:NSMakeRange(rangeStart, rangeStop)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:32.0 / 255.0 green:147.0 / 255.0 blue:138.0 / 255.0 alpha:1.0] range:NSMakeRange(rangeStart, rangeStop)];

}

UILabel *mainText = [[UILabel alloc] initWithFrame:CGRectMake(15, 70, cell.frame.size.width-10, 20)];
mainText.attributedText = str;

UILabel *detailText;

if (!detailText) {
detailText = [[UILabel alloc] initWithFrame:CGRectMake(15, 90, cell.frame.size.width-10, 30)];
}
detailText.textColor = [UIColor grayColor];
detailText.font = [UIFont systemFontOfSize:13];

switch (indexPath.row) {
case 0:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = name;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont systemFontOfSize:20];
cell.backgroundColor = [UIColor clearColor];
break;

case 1:
[cell.contentView addSubview:row2];
cell.backgroundColor = [UIColor clearColor];
break;

case 2:
cell.backgroundColor = [UIColor colorWithRed:236.0 / 255.0 green:235.0 / 255.0 blue:210.0 / 255.0 alpha:1.0];
[cell.contentView addSubview:graph];
[cell.contentView addSubview:badgets];
[cell.contentView addSubview:photos];
break;

default:
detailText.text = [[posts objectAtIndex:indexPath.row-3] objectForKey:@"_text"];
[cell.contentView addSubview:detailText];
cell.textLabel.attributedText = str;
cell.backgroundColor = [UIColor colorWithRed:236.0 / 255.0 green:235.0 / 255.0 blue:210.0 / 255.0 alpha:1.0];
break;
}


return cell; }

最佳答案

这是一种对我有用的简单方法:

for(UIView *subview in cell.contentView.subviews)
{
if([subview isKindOfClass: [UIView class]])
{
[subview removeFromSuperview];
}
}

您可以在开头使用它
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  • 关于ios - 带有 subview 的 UITableViewCell 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23301399/

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