iphone - 将不同的图像设置到 uitableview 单元格

标签 iphone objective-c uitableview

我正在制作一个应用程序,其中一个 View 有一个表格 View 。 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;
}

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

请查看我的代码并帮助我找到错误。 enter image description here

enter image description here

第二张图片是当我上下滚动表格 View 时,第一张图片位于开头。

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

关于iphone - 将不同的图像设置到 uitableview 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13818631/

相关文章:

iphone - UIScrollView 无法在 iOS7 的 iPhone 顶部屏幕中滚动

ios - 如何将值从一个 UIViewController 传递到另一个

ios - 单元格标签无法捕获表格 View 中的值

ios - 更新 UITableView 的数据

iphone - 将基于标签栏的 iPhone 项目迁移到 iPad

ios - 比较sqlite表中的整数?

ios - 正确使用 DrawRect 覆盖 - 我可以防止 View 拉伸(stretch)吗?

ios - 如何使 UITableViewCell 居中,约束无法对齐

iPhone:向用户显示简单警告/警报消息的内置方式

iphone - Facebook Connect 登录问题