0

我在情节提要中创建了一个带有四个标签的原型单元

然后在我的代码中,我为第一行、最后一行和其他行添加不同的背景图像。对于第一行,所有 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;
     }
4

3 回答 3

2

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

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

于 2013-03-01T06:40:01.213 回答
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
于 2013-11-16T11:37:50.047 回答
0

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

cell = [[[approveLeaveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
于 2013-03-01T06:32:55.507 回答