OStack程序员社区-中国程序员成长平台

标题: ios - UITableView 滚动时数据消失? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:53
标题: ios - UITableView 滚动时数据消失?

我有一个 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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4