【问题标题】:TableView data's are disappear while scrolling?TableView 数据在滚动时消失?
【发布时间】:2013-03-14 12:24:16
【问题描述】:

我尝试了很多示例但不起作用。请检查我的代码并给出解决方案。提前致谢。

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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }
if (indexPath.section==0)
{
    img=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    img.frame=CGRectMake(25, 15, 75, 75);
    img.layer.cornerRadius=6.0;
    img.clipsToBounds=YES;
    img.layer.borderColor=[[UIColor grayColor]CGColor];
    [img setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [cell.contentView addSubview:img];


    [img addTarget:self action:@selector(takePic:) forControlEvents:UIControlEventTouchDown];
    img.titleLabel.textColor=[UIColor blackColor];
    text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)];
    text.placeholder=@"Name";
    [text setBorderStyle:UITextBorderStyleRoundedRect];
    [cell addSubview:text];
    [cell addSubview:img];
    else if (indexPath.section==1)
{
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 100, 20)];
    label.text=[dosageArray objectAtIndex:indexPath.row];
    label.backgroundColor=[UIColor clearColor];
    label.font=[UIFont boldSystemFontOfSize:15.0];
    label.textColor=[UIColor blackColor];
    [cell addSubview:label];
  return cell;
     }}

我在 tableview 中有一个三个部分。在第一部分我有一个 textfield 和 imageview 如果我滚动 textfield 和 imageview 将消失,这就是问题

【问题讨论】:

  • 深入解释......我看不清楚,wt是使用else条件>?
  • 这个if (cell == nil)有什么用,不管是真是假,你都在做同样的事情。此外,您只是创建一个单元格并返回它。你在哪里分配数据?
  • 您可以在此块中发布您的文本字段和图像视图添加的代码.....

标签: iphone ios objective-c uitableview


【解决方案1】:

注意:如果您的数据有限,请使用 SOLUTION - 1,因为这不利于内存管理,否则 SOLUTION - 2 是对你有益。 (解决方案 - 1 可能对你有帮助)

解决方案 - 1

- (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:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

        cell.textLabel.text = @"NameOFData "; 

   return cell;
}

解决方案 - 2

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {   
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)  
   {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

        cell.textLabel.text = @"NameOFData "; 

   return cell;
}

已编辑:

- (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:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            if (indexPath.section==0)
           {
    img=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    img.frame=CGRectMake(25, 15, 75, 75);
    img.layer.cornerRadius=6.0;
    img.clipsToBounds=YES;
    img.layer.borderColor=[[UIColor grayColor]CGColor];
    [img setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [cell.contentView addSubview:img];


    [img addTarget:self action:@selector(takePic:) forControlEvents:UIControlEventTouchDown];
    img.titleLabel.textColor=[UIColor blackColor];
    text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)];
    text.placeholder=@"Name";
    [text setBorderStyle:UITextBorderStyleRoundedRect];
    [cell addSubview:text];
    [cell addSubview:img];
      }
        }

            cell.textLabel.text = @"NameOFData "; 

       return cell;
    }

【讨论】:

  • 把你的整个组件(imgaeView,textfield,....etch)放在...if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ; }...
【解决方案2】:

试试这段代码,将 NSMutableArray 添加到 all。这里 displayArray 是 NSMutableArray。

text.text=[displayArray objectAtIndex:1];

【讨论】:

    【解决方案3】:

    从 else 部分中删除 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 并将其留空

    享受编程!

    【讨论】:

    • ok 然后试试这个 UITableViewCell *cell = nil;静态 NSString *CellIdentifier = @"Cell";单元格 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];返回单元格;
    • @ForamMukundShah 这不是问题。因为他总是在创造一个新的细胞
    • 不,这将是一个问题,因为他正在创建新单元格,即使单元格不等于 nil
    • 哦!我刚刚查看了您的代码,发现您在大括号内写了返回单元格
    • 在大括号后写入返回单元格,否则表格将不会返回任何单元格
    【解决方案4】:

    请尝试使用这个...

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
    
              static NSString *CellIdentifier = @"Cell";
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
              if (cell == nil)
              {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    
              }
               return cell;
        }
    

    【讨论】:

      【解决方案5】:
      static NSString *CellIdentifier = @"Cell";
      
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      }
      
      cell.textLabel.text = @"Hello";
       
      return cell;
      

      【讨论】:

        【解决方案6】:
            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            
            static NSString *CellIdentifier = @"Cell";
        
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
        
        if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
            
            }
            
            cell.textLabel.text = @""// value from your datasource
            
            return cell;
            
            }
        

        【讨论】:

          【解决方案7】:

          解决方案 1

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
          {
          
          static NSString *CellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
          
          //your code
          
          }
          

          解决方案 2

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
          {
          
          static NSString *CellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
          
          }
          
          cell.textLabel.text= @"YOUR TEXT";
          
          }
          

          希望这会对你有所帮助。

          一切顺利!!!

          【讨论】:

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