gpt4 book ai didi

ios - UITableView 滚动时数据消失?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:46 27 4
gpt4 key购买 nike

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

这是我的cellForRowAtIndexPath:-

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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
constant:(cell.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;
}

最佳答案

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

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)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)configureFareIssueCell:(YOUR_TABLEVIEW_CELL* )cell atIndexPath:(NSIndexPath *)indexPath
{
//e.g
cell.lbl.text=@"YOUR_TEXT";
cell.imageView.image=[UIImage imageNamed:@"NAME_OF_YOUR_IMAGE"];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com