Reputation: 975
I have a UITableView
in which i am fetching data from server and displaying it in the UITableViewCell
. But data disappears while scrolling . Please help .
Here is my 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;
}
Upvotes: 2
Views: 2485
Reputation: 858
You are changing constraints at runtime.
[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]];
Instead of this, just add auto layout constraints form storyboard to your label. And in cellForRowAtIndexPath
method just update constraints according to your requirement.
Upvotes: 0
Reputation: 1942
Try Using
[tableView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
instead of
[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Upvotes: 1
Reputation: 304
You are having cell dequeue issue....Yor are not getting proper cell height. Follow the following method for assigning data & getting proper height. Check your cell having proper height
-(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;
}
Upvotes: 1