TableView单元格标签在滚动时消失,但对于选定的单元格是否可见?

时间:2013-02-20 09:14:34

标签: iphone objective-c uitableview

由于TableView单元格标签在加载时显示,但是当滚动tableView时,tableview内容不会显示。但是,如果我选择任何一个单元格,那么该特定单元格中的标签就会显示出来。

请帮帮我。我在不同方面也有同样的问题。我没有得到任何解决方案。

这是我的cellForRowAtIndexPath方法代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    static NSString *CellIdentifier = @"newsTableCell";
        UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil) {       
                            cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            }
            UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
            user.tag = 101;
            [cell.contentView addSubview:user];
            [user release];
            UILabel *usr = (UILabel *)[cell viewWithTag:101];
            usr.font = [UIFont boldSystemFontOfSize:12.0];
            [usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];

            UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
            status.tag = 102;
            [cell.contentView addSubview:status];
            [status release];
            status = (UILabel *)[cell viewWithTag:102];
            status.font = [UIFont boldSystemFontOfSize:12.0];
            [status setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
            UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
            rival.tag = 103;
            [cell.contentView addSubview:rival];
            [rival release];
            UILabel *rivals = (UILabel *)[cell viewWithTag:103];
            rivals.font = [UIFont boldSystemFontOfSize:12.0];
            [rivals setText:[appDelegate.rivalusernames objectAtIndex:[indexPath row]]];
            UILabel *gamename = [[UILabel alloc] initWithFrame:CGRectMake(84,27,208,21)];
            gamename.tag = 104;
            [cell.contentView addSubview:gamename];
            [gamename release];
            UILabel *gamenames = (UILabel *)[cell viewWithTag:104];
            gamenames.font = [UIFont boldSystemFontOfSize:12.0];
            [gamenames setText:[appDelegate.challengenames objectAtIndex:[indexPath row]]];
            UILabel *time = [[UILabel alloc] initWithFrame: CGRectMake(84,47,149,21)];
            time.tag = 105;
            [cell.contentView addSubview:time];
            [time release];
            UILabel *times = (UILabel *)[cell viewWithTag:105];
            times.font = [UIFont boldSystemFontOfSize:12.0];
            [times setText:[appDelegate.remainingtime objectAtIndex:[indexPath row]]];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
            imageView.tag = 106;
            [cell.contentView addSubview:imageView];
            [imageView release];
            NSURL *url = [NSURL URLWithString:[appDelegate.imagepaths objectAtIndex:[indexPath row]]];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *img = [[UIImage alloc] initWithData:data];
            UIImageView *imgView = (UIImageView *)[cell viewWithTag:106];
            imgView.image = img;
            return cell;
        }

4 个答案:

答案 0 :(得分:1)

首先将这些行移到if(cell == NIL):

    if(cell == nil) {       
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault       reuseIdentifier:CellIdentifier] autorelease];
        UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
        user.tag = 101;
        [cell.contentView addSubview:user];
        [user release];

    }

或者每次重复使用时都会向单元格添加子视图,滚动

和以下相同:

UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
            status.tag = 102;
            [cell.contentView addSubview:status];
            [status release];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
            imageView.tag = 106;
            [cell.contentView addSubview:imageView];
            [imageView release];

......等等

作为一般规则:在第一次创建单元格时(在if(cell == nil)内部)

只添加/分配一次新对象

然后在外面,如果你只是重用所有对象并改变它们的属性,作为图像源的文本......

答案 1 :(得分:1)

也许你的UILabel的颜色和背景颜色一样?当您选择一个单元格然后您可以看到文本时,可能是因为当时突出显示的单元格颜色...

我还强烈建议你创建一个像cortez提到的细胞子类,这段代码不是可读的,也不是可维护的......

答案 2 :(得分:0)

你的UITableView Cell的实现是这样尝试的 我知道你的问题会解决。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
     static NSString *CellIdentifier = @"newsTableCell";

 UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) 
{       
     cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
        user.tag = 101;
      usr.font = [UIFont boldSystemFontOfSize:12.0];           
      [cell.contentView addSubview:user];
        [user release];
        UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
        status.tag = 102;
      status.font = [UIFont boldSystemFontOfSize:12.0];
        [cell.contentView addSubview:status];
        [status release];
        UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
        rival.tag = 103;
        [cell.contentView addSubview:rival];
        [rival release];
}
else
{
    UILabel *usr = (UILabel *)[cell viewWithTag:101];
    status = (UILabel *)[cell viewWithTag:102];
    rival = (UILabel *)[cell viewWithTag:103];
}
[usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];
[status setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
[rival setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
return cell;

}

答案 3 :(得分:0)

最初我写了上面的代码。在meronix的建议之后,我已经改为一个以下。请建议。

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];    
    if (tableView.tag ==16)
    {
        NSLog(@"%d",appDelegate.checkChallenges);
        static NSString *CellIdentifier = @"newsTableCell";

        UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil) {
            cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
            user.tag = 101;
            [cell.contentView addSubview:user];
            [user release];
            UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
            status.tag = 102;
            [cell.contentView addSubview:status];
            [status release];
            UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
            rival.tag = 103;
            [cell.contentView addSubview:rival];
            //[rival release];
            UILabel *gamename = [[UILabel alloc] initWithFrame:CGRectMake(84,27,208,21)];
            gamename.tag = 104;
            [cell.contentView addSubview:gamename];
            [gamename release];

            UILabel *time = [[UILabel alloc] initWithFrame: CGRectMake(84,47,149,21)];
            time.tag = 105;
            [cell.contentView addSubview:time];
            [time release];

            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
            imageView.tag = 106;
            [cell.contentView addSubview:imageView];
            [imageView release];



        }

        UILabel *usr = (UILabel *)[cell viewWithTag:101];
        usr.font = [UIFont boldSystemFontOfSize:12.0];
        [usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];


        UILabel *stat = (UILabel *)[cell viewWithTag:102];
        stat.font = [UIFont boldSystemFontOfSize:12.0];
        [stat setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];

        NSURL *url = [NSURL URLWithString:[appDelegate.imagepaths objectAtIndex:[indexPath row]]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        UIImageView *imgView = (UIImageView *)[cell viewWithTag:106];
        imgView.image = img;

        UILabel *times = (UILabel *)[cell viewWithTag:105];
        times.font = [UIFont boldSystemFontOfSize:12.0];
        [times setText:[appDelegate.remainingtime objectAtIndex:[indexPath row]]];

        UILabel *gamenames = (UILabel *)[cell viewWithTag:104];
        gamenames.font = [UIFont boldSystemFontOfSize:12.0];
        [gamenames setText:[appDelegate.challengenames objectAtIndex:[indexPath row]]];

        UILabel *rivals = (UILabel *)[cell viewWithTag:103];
        rivals.font = [UIFont boldSystemFontOfSize:12.0];
        [rivals setText:[appDelegate.rivalusernames objectAtIndex:[indexPath row]]];

        return cell;

    }