Search code examples
iphoneuitableviewios6

content in tableview cell overlapping


I created a prototype cell in storyboard with four labels

then in my code i am adding different background images for first,last and other rows. for first row all 4 labels are hidden.

Intially all 5 rows shown in screen appear good.but when i scroll up to see 6th row ,it behaves like first row and all 4 labels are hidden.

here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{

Employee *currentEmployee=[Employee sharedData];
NSArray *tempArray =currentEmployee.leaveApproveDict;
static NSString *simpleTableIdentifier = @"approveLeaveCell";


approveLeaveCell *cell = [self.approveLeaveView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
    cell = [[approveLeaveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}


if([tempArray count]!=0){
    if(indexPath.row == 0){
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"h_t_top_bar.png"]];
        cell.backgroundView = imgView;
        cell.fromLabel.hidden=YES;
        cell.toLabel.hidden=YES;
        cell.typeLabel.hidden=YES;
        cell.nameLabel.hidden=YES;
        [cell setUserInteractionEnabled:NO];
    }
    else{
        if(indexPath.row ==[tempArray count]){
            UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"h_t_bottom_bar.png"]];
            cell.backgroundView = imgView;
        }
        else{
            UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"h_t_repeat_bar.png"]];
            cell.backgroundView = imgView;

        }

        //assign the data
        if(indexPath.row-1 < [tempArray count]){
            NSDictionary *tempLeave= [currentEmployee.leaveApproveDict objectAtIndex:indexPath.row-1];
            cell.toDateLabel.text=[self extractLeaveDate:(NSString*) [tempLeave objectForKey:@"leaveTo"]];
            cell.fromDateLabel.text=[self extractLeaveDate:(NSString*)[tempLeave objectForKey:@"leaveFrom"]];
            cell.leaveLabel.text=[tempLeave objectForKey:@"leaveType"];
            cell.employeeNameLabel.text=[tempLeave objectForKey:@"requesterName"];

    }
}

return cell;
     }

Solution

  • The same issue happens with me. I have solved it using two different customized UITableViewCell and two identifiers.

    A UITableView object maintains a queue (or list) of the currently reusable cells, each with its own reuse identifier, and makes them available to the delegate in the dequeueReusableCellWithIdentifier: method.