1

由于 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

4 回答 4

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) 内),您只需添加/分配新对象一次

然后除此之外,如果您只是重用所有对象并更改它们的属性,作为图像源的文本......

于 2013-02-20T09:20:32.517 回答
1

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

而且我还强烈建议您像 cortez 提到的那样创建一个单元子类,此代码不可读取,也不可维护...

于 2013-02-20T10:25:49.767 回答
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;

}

于 2013-02-20T10:42:01.123 回答
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;

    }
于 2013-02-20T10:48:18.960 回答