【问题标题】:set different images to uitableview cell将不同的图像设置为 uitableview 单元格
【发布时间】:2012-12-11 10:47:25
【问题描述】:

我正在制作一个应用程序,其中一个视图有一个表格视图。 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 单元格。

提前致谢。

【问题讨论】:

标签: iphone objective-c uitableview


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    首先,如果您计划添加子视图,您应该真正使用UITableViewCell 的子类(有关更多信息,请参阅Apple's documentation)。但是,我离题了...

    有一种更简单的方法可以将图像添加到您的单元格(使用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);
    
        ...
    }
    

    【讨论】:

    【解决方案3】:

    试试这个,你必须在调用创建之前放置 }

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

    希望对你有所帮助..

    【讨论】:

      【解决方案4】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多