我有一个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;
}
答案 0 :(得分:1)
你有细胞出队问题....你没有得到适当的细胞高度。按照以下方法分配数据&获得适当的高度。检查你的牢房有适当的高度
-(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;
}
答案 1 :(得分:1)
尝试使用
[tableView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
而不是
[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
答案 2 :(得分: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]];
而不是这样,只需将自动布局约束表单添加到标签中。在cellForRowAtIndexPath
方法中,只需根据您的要求更新约束。