2

ビューの1つにテーブルビューがあるアプリを作成しています。Tableviewセルには2つの条件があります。条件に応じてuitableviewセルに設定される2つの画像があります。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *que =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"question"];
    NSString *ans =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"answer"];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:CellIdentifier] autorelease];
        cell.imageView.image=nil;

        if ((que.length!=0)&&(ans.length!=0)) {
            UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 75)];
            imag.image = [UIImage imageNamed:@"ques.png"];
            [cell.contentView addSubview:imag];
            questext = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
            questext.backgroundColor = [UIColor clearColor];
            questext.delegate = self;
            questext.tag = 101;
            questext.textAlignment = UITextAlignmentLeft;
            questext.editable = NO;
            questext.scrollEnabled = YES;
            [cell addSubview:questext];

            anstext = [[UITextView alloc]initWithFrame:CGRectMake(10, 37, 300, 35)];
            anstext.backgroundColor = [UIColor clearColor];
            anstext.delegate = self;
            anstext.tag = 102;
            anstext.scrollEnabled = YES;
            anstext.textAlignment = UITextAlignmentLeft;
            anstext.editable = NO;
            [cell addSubview:anstext];
        }
        else {
            UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
            imag.image = [UIImage imageNamed:@"answ.png"];
            [cell.contentView addSubview:imag];

            onlyques = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
            onlyques.backgroundColor= [UIColor clearColor];
            [onlyques setScrollEnabled:YES];
            onlyques.delegate = self;
            onlyques.tag = 103;
            onlyques.textAlignment = UITextAlignmentLeft;
            onlyques.editable = NO;
            onlyques.scrollEnabled = YES;
            [cell addSubview:onlyques];
         }
    }
    questext = (UITextView*)[cell viewWithTag:101];
    questext.text = que;

    anstext = (UITextView*)[cell viewWithTag:102];
    anstext.text = ans;

    onlyques = (UITextView*)[cell viewWithTag:103];
    onlyques.text = que;

    return cell;
}

しかし、画像が正しく表示されていません。テーブルビューを上下にスクロールすると、画像が自動的に変更されます。

私のコードを見て、エラーを見つけるのを手伝ってください。ここに画像の説明を入力してください

ここに画像の説明を入力してください

2番目の画像は、テーブルビューを上下にスクロールしたときの画像で、最初の画像は最初の画像です。

私を助けてください。uitableviewセルにさまざまな画像をロードする方法を知っている人がいる場合。

前もって感謝します。

4

4 に答える 4

2

これを試して :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *que =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"question"];
   NSString *ans =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"answer"];
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil)
   {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:CellIdentifier] autorelease];
      cell.imageView.image=nil;
   } 
   else
   {
      [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
   }

   if ((que.length!=0)&&(ans.length!=0)) 
   {
        UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 75)];
        imag.image = [UIImage imageNamed:@"ques.png"];
        [cell.contentView addSubview:imag];
        questext = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
        questext.backgroundColor = [UIColor clearColor];
        questext.delegate = self;
        questext.tag = 101;
        questext.textAlignment = UITextAlignmentLeft;
        questext.editable = NO;
        questext.scrollEnabled = YES;
        [cell addSubview:questext];

        anstext = [[UITextView alloc]initWithFrame:CGRectMake(10, 37, 300, 35)];
        anstext.backgroundColor = [UIColor clearColor];
        anstext.delegate = self;
        anstext.tag = 102;
        anstext.scrollEnabled = YES;
        anstext.textAlignment = UITextAlignmentLeft;
        anstext.editable = NO;
        [cell addSubview:anstext];
    }
    else 
    {
        UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        imag.image = [UIImage imageNamed:@"answ.png"];
        [cell.contentView addSubview:imag];

        onlyques = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
        onlyques.backgroundColor= [UIColor clearColor];
        [onlyques setScrollEnabled:YES];
        onlyques.delegate = self;
        onlyques.tag = 103;
        onlyques.textAlignment = UITextAlignmentLeft;
        onlyques.editable = NO;
        onlyques.scrollEnabled = YES;
        [cell addSubview:onlyques];
     }

     questext = (UITextView*)[cell viewWithTag:101];
     questext.text = que;

     anstext = (UITextView*)[cell viewWithTag:102];
     anstext.text = ans;

     onlyques = (UITextView*)[cell viewWithTag:103];
     onlyques.text = que;

     return cell;
}
于 2012-12-11T11:17:45.107 に答える
1

まず、UITableViewCellサブビューを追加する予定がある場合は、のサブクラスを実際に使用する必要があります(詳細については、Appleのドキュメントを参照してください)。しかし、私は逸脱します...

UITableViewCellStyleDefault(を使用して)セルに画像を追加する簡単な方法があります。

if (!cell)
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

if ((que.length!=0)&&(ans.length!=0)) {
    cell.imageView.image = [UIImage imageNamed:@"ques.png"];
    cell.imageView.frame = CGRectMake(0, 0, 320, 75);

    ...
}
else {

    cell.imageView.image = [UIImage imageNamed:@"answ.png"];
    cell.imageView.frame = CGRectMake(0, 0, 320, 40);

    ...
}
于 2012-12-11T11:02:00.693 に答える
0

これを試して

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath{
NSString *que =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"question"];
NSString *ans =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"answer"];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    cell.imageView.image=nil;
}
    if ((que.length!=0)&&(ans.length!=0)) {
        UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 75)];
        imag.image = [UIImage imageNamed:@"ques.png"];
        [cell.contentView addSubview:imag];
        questext = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
        questext.backgroundColor = [UIColor clearColor];
        questext.delegate = self;
        questext.tag = 101;
        questext.textAlignment = UITextAlignmentLeft;
        questext.editable = NO;
        questext.scrollEnabled = YES;
        [cell addSubview:questext];

        anstext = [[UITextView alloc]initWithFrame:CGRectMake(10, 37, 300, 35)];
        anstext.backgroundColor = [UIColor clearColor];
        anstext.delegate = self;
        anstext.tag = 102;
        anstext.scrollEnabled = YES;
        anstext.textAlignment = UITextAlignmentLeft;
        anstext.editable = NO;
        [cell addSubview:anstext];
    }

    else
    {
        UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        imag.image = [UIImage imageNamed:@"answ.png"];
        [cell.contentView addSubview:imag];

        onlyques = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
        onlyques.backgroundColor= [UIColor clearColor];
        [onlyques setScrollEnabled:YES];
        onlyques.delegate = self;
        onlyques.tag = 103;
        onlyques.textAlignment = UITextAlignmentLeft;
        onlyques.editable = NO;
        onlyques.scrollEnabled = YES;
        [cell addSubview:onlyques];
     }

questext = (UITextView*)[cell viewWithTag:101];
questext.text = que;

anstext = (UITextView*)[cell viewWithTag:102];
anstext.text = ans;

onlyques = (UITextView*)[cell viewWithTag:103];
onlyques.text = que;

return cell;

}

私がここでやったことは、あなたの画像の変化する状態を外に出すことです

if(cell == nil){ } statement
于 2012-12-11T10:49:53.080 に答える
0

これを試してください。呼び出しが作成される前に}を配置する必要があります

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   
      *)indexPath


       {
    NSString *que =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"question"];
    NSString *ans =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"answer"];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   
     reuseIdentifier:CellIdentifier] autorelease];
    }
        cell.imageView.image=nil;

        if ((que.length!=0)&&(ans.length!=0)) {
            UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 75)];
            imag.image = [UIImage imageNamed:@"ques.png"];
            [cell.contentView addSubview:imag];
            questext = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
            questext.backgroundColor = [UIColor clearColor];
            questext.delegate = self;
            questext.tag = 101;
            questext.textAlignment = UITextAlignmentLeft;
            questext.editable = NO;
            questext.scrollEnabled = YES;
            [cell addSubview:questext];

            anstext = [[UITextView alloc]initWithFrame:CGRectMake(10, 37, 300, 35)];
            anstext.backgroundColor = [UIColor clearColor];
            anstext.delegate = self;
            anstext.tag = 102;
            anstext.scrollEnabled = YES;
            anstext.textAlignment = UITextAlignmentLeft;
            anstext.editable = NO;
            [cell addSubview:anstext];
        }

        else
        {
            UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
            imag.image = [UIImage imageNamed:@"answ.png"];
            [cell.contentView addSubview:imag];

            onlyques = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
            onlyques.backgroundColor= [UIColor clearColor];
            [onlyques setScrollEnabled:YES];
            onlyques.delegate = self;
            onlyques.tag = 103;
            onlyques.textAlignment = UITextAlignmentLeft;
            onlyques.editable = NO;
            onlyques.scrollEnabled = YES;
            [cell addSubview:onlyques];
         }

    questext = (UITextView*)[cell viewWithTag:101];
    questext.text = que;

    anstext = (UITextView*)[cell viewWithTag:102];
    anstext.text = ans;

    onlyques = (UITextView*)[cell viewWithTag:103];
    onlyques.text = que;

    return cell;

}

これがお役に立てば幸いです。

于 2012-12-11T10:56:53.663 に答える