tableview单元格中的内容重叠

时间:2013-03-01 06:25:55

标签: iphone uitableview ios6

我在故事板中创建了一个带有四个标签的原型单元

然后在我的代码中,我为第一行,最后一行和其他行添加不同的背景图像。 对于第一行,隐藏所有4个标签。

屏幕中显示的所有5行显示都很好。但是当我向上滚动以查看第6行时,它的行为与第一行相似,并且所有4个标签都被隐藏。

这是我的代码:

- (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;
     }

3 个答案:

答案 0 :(得分:2)

同样的问题发生在我身上。我使用两个不同的自定义UITableViewCell和两个标识符解决了它。

UITableView对象维护当前可重用单元的队列(或列表),每个单元都有自己的重用标识符,并使dequeueReusableCellWithIdentifier:方法中的委托可以使用它们。

答案 1 :(得分:0)

您在项目中使用过ARC吗?如果你没有使用arc,那么cell应该使用autorelease:

cell = [[[approveLeaveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];

答案 2 :(得分:0)

 /* Remove all existing content from list and reload them again to avoid 
     * overlay of cell content */

    for(UIView *view in cell.contentView.subviews){
        if ([view isKindOfClass:[UIView class]]) {
            [view removeFromSuperview];
        }
    }
// VKJ
相关问题