• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UITableView 滚动时数据消失?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:53:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 UITableView,我在其中从服务器获取数据并将其显示在 UITableViewCell 中。但是滚动时数据会消失。请帮忙 。

这是我的 cellForRowAtIndexPath :-

-(UITableViewCell*)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"CellIdentifier";
    CCCustomCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    //UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];

    if (cell==nil) {
        cell=[[CCCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }


    cell.keyText.text=[[[[arraySuper objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] allKeys] objectAtIndex:0];
    cell.objectText.text=[[[[arraySuper objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] allObjects] objectAtIndex:0];

    [cell.objectText addConstraint:[NSLayoutConstraint constraintWithItem:cell.objectText
                                                                attribute:NSLayoutAttributeWidth
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:NSLayoutAttributeHeight
                                                               multiplier:1.0
                                                                 constantcell.objectText.text.length>0)?50:0]];

    [cell setUserInteractionEnabled:YES];
    tableView.allowsSelection=YES;

      if (indexPath.row==0)
    {
        cell.keyText.textColor=[UIColor colorWithRed:62.0/255.0 green:60.0/255.0 blue:100.0/255.0 alpha:1];
        cell.keyText.font=[UIFont fontWithName"Helvetica-Bold" size:15];
        [cell setUserInteractionEnabled:YES];

    }
    else
    {
        cell.keyText.textColor=[UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
        cell.keyText.font=[UIFont fontWithName"Helvetica" size:13];
        cell.objectText.textColor=[UIColor colorWithRed:62.0/255.0 green:60.0/255.0 blue:100.0/255.0 alpha:1];
        cell.objectText.font=[UIFont fontWithName"Helvetica" size:13];
        cell.userInteractionEnabled=YES;

    }

       return cell;
}



Best Answer-推荐答案


您遇到了单元格出列问题....您没有获得正确的单元格高度。按照以下方法分配数据并获得适当的高度。检查您的单元格的高度是否合适

-(CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
    static YOUR_TABLEVIEW_CELL *sizingCell = nil;
    static NSString *CellIdentifier=@"YOUR_TABLEVIEW_CELL_IDENTIFIER";
    sizingCell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (sizingCell==nil)
   {
       sizingCell=[[YOUR_TABLEVIEW_CELL alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }
    [self configureFareIssueCell:sizingCell atIndexPath:indexPath];
    return [self calculateHeightForConfiguredSizingCell:sizingCell];
}

//assign all the labels  here
- (void)configureFareIssueCellYOUR_TABLEVIEW_CELL* )cell atIndexPathNSIndexPath *)indexPath
{
    //e.g 
   cell.lbl.text=@"YOUR_TEXT";
   cell.imageView.image=[UIImage imageNamed"NAME_OF_YOUR_IMAGE"];
} 

- (CGFloat)calculateHeightForConfiguredSizingCellYOUR_TABLEVIEW_CELL *)sizingCell
{
    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
   return size.height + 1.0f; // Add 1.0f for the cell separator height
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
  static NSString *CellIdentifier=@"YOUR_TABLEVIEW_CELL_IDENTIFIER";
  YOUR_TABLEVIEW_CELL   *cell =[tableView dequeueReusableCellWithIdentifier"YOUR_TABLEVIEW_CELL_IDENTIFIER"];
if (cell==nil)
{
    cell=[[YOUR_TABLEVIEW_CELL alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }
   [self configureFareIssueCell:cell atIndexPath:indexPath];
  return cell;
 }   

关于ios - UITableView 滚动时数据消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31578625/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap