1

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];

...など

原則として、最初にセルを作成するときに、新しいオブジェクトを1回だけ追加/割り当てます(if(cell == nil)内)

それ以外では、すべてのオブジェクトを再利用してプロパティを変更すると、画像ソースのテキストとして...

于 2013-02-20T09:20:32.517 に答える
1

たぶんあなたのUILabelsの色は背景色と同じですか?セルを選択するとテキストが表示されますが、これはおそらく、セルの色が強調表示されているためです...

また、cortezが述べたように、セルのサブクラスを作成することを強くお勧めします。このコードは読み取りも保守もできません...

于 2013-02-20T10:25:49.767 に答える
0

UITableView セルの実装は正しいです。この方法を試してみてください。問題が解決することを知っています。

- (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 に答える