2

我正在制作一个应用程序,其中一个视图有一个表格视图。Tableview 单元格有两个条件。根据条件,将在 uitableview 单元格上设置两个图像,即

 - (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;
}

但图像显示不正确。当我上下滚动表格视图时,图像会自动更改。

请查看我的代码并帮助我找到错误。在此处输入图像描述

在此处输入图像描述

第二张图片是当我向上和向下滚动表格视图时,第一张图片是在开始时。

请帮我。如果有人知道如何将不同的图像加载到 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;

}

希望这可以帮助你..

于 2012-12-11T10:56:53.663 回答
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 回答