带有子视图的UITableViewCell无法正常工作

时间:2014-04-25 19:26:20

标签: ios objective-c uitableview

我的iOS应用程序中有UITableView,我想在单元格中添加一些子视图。我是通过使用

来做到的

[cell.contentView addSubview:someView];

并且效果很好,但是...当我向下滚动时,子视图开始隐藏在顶部的单元格中,当我滚动回顶部时,它们不会再出现......我在做什么错误?请问有什么解决方案吗?

修改

主要是,我正在谈论" 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; }

3 个答案:

答案 0 :(得分:2)

这对我很有用:

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

您可以在

的开头使用它
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

答案 1 :(得分:1)

在你的tableView:cellForRowAtIndexPath:中,当你不希望它显示时隐藏信息,但是你没有明确取消隐藏它应该显示它的单元格。

查看该方法中的前两行:你是什么 - 正确 - 做的是重用你的单元格,所以当单元格滚出视图时,它们将从UITableView中删除并放入重用队列。然后,当单元格变得可见时,TableView将从该队列中获取单元格 - 如果没有可用单元格,则创建新单元格。

这一切都很顺利,但过了一会儿,带有隐藏信息按钮的单元格会被放入队列中。然后,一段时间之后,这些单元格被重用 - 有时候对于应该有信息可见的行。

有两种解决方案:您可以显式取消隐藏您希望显示的行的信息,也可以使用两种不同类型的单元格,一种具有隐藏信息,另一种具有可见信息。然后,为每个单元格提供不同的标识符,并根据单元格所在的行,在出列/创建单元格之前设置标识符。

答案 2 :(得分:1)

您应该为每个不同的单元格创建UITableViewCell的子类,并将根据数据不会更改的所有视图相关代码添加到初始化方法中。然后在每个单元格中创建一个名为configureWithData的方法,并传入与单元格相关的数据。在此配置方法中可以创建属性字符串和修改标签帧。

它将大大减少UITableViewController中的混乱,并且设计明智得多。您的视图控制器不需要知道您的表格单元格是什么样的。

以下是我所说的一个例子:

-(void)awakeFromNib
{
    if( self.accessoryType == UITableViewCellAccessoryDisclosureIndicator )
    {
        DTCustomColoredAccessory *accessory = [DTCustomColoredAccessory accessoryWithColor:[UIColor whiteColor]];
        accessory.highlightedColor = [UIColor blackColor];
        self.accessoryView = accessory;
    }
}

-(void)configureCellWithObject:(id)inObject
{
    TableDataModel *dataObject = (TableDataModel *)inObject;
    self.titleLabel.text = dataObject.titleString;
    self.subtitleLabel.text = dataObject.subtitleString;
    self.accessibilityIdentifier = dataObject.accessIdString;

    if( dataObject.imageUrlString != nil )
    {
        UIImage *iconImage = [UIImage imageNamed:dataObject.imageUrlString];
        if( iconImage != nil )
        {
            NSInteger yOffset = [StaticTools centerYOffset:self.frame.size objectFrameSize:iconImage.size];
            self.iconImageView.image = iconImage;
            CGRect frame = self.iconImageView.frame;
            frame.origin.y = yOffset;
            frame.size = iconImage.size;
            [self.iconImageView setFrame:frame];
        }
        else
        {
            [self.iconImageView loadImageFromUrl:dataObject.imageUrlString];
        }
    }
}
相关问题