【问题标题】:UITextView text in Tableview get Overrides?Tableview 中的 UITextView 文本会被覆盖吗?
【发布时间】:2012-10-05 11:16:25
【问题描述】:

在我的应用程序中,我使用四个 UITextView 自定义了 UITableView 单元。每当我将数据添加到 tableview 并重新加载它时。 UITableViewCell 中的文本会被之前的文本覆盖。

我尝试了不同的方法,但无法找出问题所在。

我在 View Controller 中使用 TableView。

这是我在表格视图单元格中使用的代码?

if ( [tableView isEqual:self.tableActions])
{
    //Setting the text empty

    static NSString *CellIdentifier = @"ActionsIdentifier";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }
    NSLog(@"Index Path %i",[indexPath row]);
    ActionDetails *actiondetails = [actionArray objectAtIndex:[indexPath row]];

    NSLog(@"Action Text %@",actiondetails.actions);

    //Actions
    actionText=[[UITextView alloc] initWithFrame:CGRectMake(10, 5, 230,30)];
    actionText.font = [UIFont systemFontOfSize:17.0];
    actionText.editable = NO;
    actionText.textColor=[UIColor blackColor];
    actionText.text = actiondetails.actions ; 
    actionText.userInteractionEnabled = NO;
    actionText.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:actionText];


    //Owner         
    ownerBy=[[UITextView alloc] initWithFrame:CGRectMake(230, 5, 230,30)];
    ownerBy.font = [UIFont systemFontOfSize:17.0];
    ownerBy.textColor=[UIColor blackColor];
    ownerBy.textAlignment = UITextAlignmentCenter;
    ownerBy.text = actiondetails.owner;
    ownerBy.editable = NO;
    ownerBy.userInteractionEnabled = NO;
    ownerBy.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:ownerBy];

 }

屏幕截图

感谢你们的帮助。

非常感谢。

【问题讨论】:

  • 感谢您的回答,伙计们 +1 对您的所有支持..
  • 你得到这个解决方案了吗?我也有同样的问题

标签: iphone ios uitableview


【解决方案1】:

这件事前几天发生在我身上,我想出的解决方案是在 if 语句中创建单元格后从单元格中删除所有子视图。

if(cell == nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
if ([cell.contentView subviews]){
    for (UIView *subview in [cell.contentView subviews]) {
        [subview removeFromSuperview];
    }
}

【讨论】:

    【解决方案2】:

    删除 Tableview 的单元格标识符,否则将 Customcell 用于 Tableview..

    【讨论】:

    • 感谢您的回复。抱歉,您是什么意思删除单元格标识符或为 tableview 使用 customcell
    • 在您的代码中,您已将 cellidentifier 删除并将其设置为 nil .. cell = [tableView dequeueReusableCellWithIdentifier:nil];cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    • 您正在重复使用单元格,这就是问题发生的原因
    • 另一种方法是安迪保罗的回答..见下文
    • 嗨,当我在单元格标识符中使用 nil 时,单击事件在 tableview 中不起作用
    【解决方案3】:

    试试这个方法很简单:

     {
            //Setting the text empty
    
            static NSString *CellIdentifier = @"ActionsIdentifier";
    
            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
            **// if(cell == nil) // comment this line in your code ,its work 
           // {**
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
           **// }**
            NSLog(@"Index Path %i",[indexPath row]);
            ActionDetails *actiondetails = [actionArray objectAtIndex:[indexPath row]];
    
            NSLog(@"Action Text %@",actiondetails.actions);
    
            //Actions
            actionText=[[UITextView alloc] initWithFrame:CGRectMake(10, 5, 230,30)];
            actionText.font = [UIFont systemFontOfSize:17.0];
            actionText.editable = NO;
            actionText.textColor=[UIColor blackColor];
            actionText.text = actiondetails.actions ; 
            actionText.userInteractionEnabled = NO;
            actionText.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:actionText];
    
    
            //Owner         
            ownerBy=[[UITextView alloc] initWithFrame:CGRectMake(230, 5, 230,30)];
            ownerBy.font = [UIFont systemFontOfSize:17.0];
            ownerBy.textColor=[UIColor blackColor];
            ownerBy.textAlignment = UITextAlignmentCenter;
            ownerBy.text = actiondetails.owner;
            ownerBy.editable = NO;
            ownerBy.userInteractionEnabled = NO;
            ownerBy.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:ownerBy];
    
         }
    

    【讨论】:

      【解决方案4】:

      您正在重复使用该单元格。重用的单元格已经添加了 UITextview。所以你正在覆盖它。 您必须将创建和添加 UITextView 的所有代码转移到。

      如果(单元格==nil) {

      }

      之后,您只需将 Text 设置为 UITextview。

      【讨论】:

        【解决方案5】:

        当您的视图确实出现时,请重新加载您的表格视图。

        -(void)viewDidAppear:(BOOL)animated {    
        [tableView reloadData]; 
        }
        

        【讨论】:

          【解决方案6】:

          改变你的cellForRowAtIndexPath 方法

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
              {
          
                  NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
          
                  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                  if (cell == nil)
                  {
                      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          
                     // your code 
          
                  }
          
                    // your code 
          
                  return cell;    
          
              }
          

          【讨论】:

            【解决方案7】:
            for (id vw in [[cell contentView] subviews]) {
                if ([vw isKindOfClass:[UILabel class]])
                {
                   UILabel *label = (UILabel *)vw;
                   [label removeFromSuperview];
                }
             }
            
             for (id vw in [cell subviews]) {
                 if ([vw isKindOfClass:[UILabel class]])
                 {
                    UILabel *label = (UILabel *)vw;
                    [label removeFromSuperview];
                 }
             }
            

            【讨论】:

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