Iphone tableview单元格中的内容重叠

Iphone tableview单元格中的内容重叠,iphone,uitableview,ios6,Iphone,Uitableview,Ios6,我在故事板中创建了一个带有四个标签的原型单元 然后在我的代码中,我为第一行、最后一行和其他行添加了不同的背景图像。 对于第一行,所有4个标签均隐藏 最初,屏幕上显示的所有5行看起来都不错。但当我向上滚动查看第6行时,它的行为就像第一行一样,所有4个标签都隐藏了 这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Em

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

然后在我的代码中,我为第一行、最后一行和其他行添加了不同的背景图像。 对于第一行,所有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;
     }
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
员工*currentEmployee=[Employee sharedData];
NSArray*tempArray=currentEmployee.leaveApproveDict;
静态NSString*simpleTableIdentifier=@“approveLeaveCell”;
approveLeaveCell*单元格=[self.approveLeaveView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil){
cell=[[approveLeaveCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:simpleTableIdentifier];
}
如果([tempArray count]!=0){
if(indexPath.row==0){
UIImageView*imgView=[[UIImageView alloc]initWithImage:[UIImageName:@“h_t_top_bar.png”];
cell.backgroundView=imgView;
cell.fromLabel.hidden=是;
cell.toLabel.hidden=是;
cell.typeLabel.hidden=是;
cell.namelab.hidden=是;
[cell setUserInteractionEnabled:否];
}
否则{
if(indexPath.row==[tempArray count]){
UIImageView*imgView=[[UIImageView alloc]initWithImage:[UIImageName:@“h_t_bottom_bar.png”];
cell.backgroundView=imgView;
}
否则{
UIImageView*imgView=[[UIImageView alloc]initWithImage:[UIImageName:@“h_t_repeat_bar.png”];
cell.backgroundView=imgView;
}
//分配数据
if(indexath.row-1<[tempArray count]){
NSDictionary*tempLeave=[currentEmployee.leaveApproveDict objectAtIndex:indexath.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”];
}
}
返回单元;
}

项目中是否使用了ARC?如果未使用ARC,则单元格应使用自动释放:

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

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


UITableView
对象维护队列(或列表)在当前可重用单元格中,每个单元格都有自己的重用标识符,并在
dequeueReusableCellWithIdentifier
:方法中使代理可以使用这些单元格。

我以前曾尝试创建两个不同的自定义单元格,但没有成功,……在您的建议后,我再次尝试,但现在在第行时添加了另一个return语句是零……它解决了我的问题……感谢您的输入……现在我在点击表格视图单元格时添加了复选标记,但在其他单元格中,当我滚动uitableviewcell中多种类型的行时,复选标记会在其他单元格中重复。uitableviewcell中多种类型行的规则是您使用具有不同标识符的不同自定义单元格。然后不会重复。将复选标记用作ima自定义单元格中的ge视图我修复了它,我将选定单元格的indexpath添加到数组中,然后在CellForRowatinex中:如果indexpath在数组中,我将显示复选标记。对于多行选择,数组可以,但如果要单行选择,则使用int或bool。
[视图isKindOfClass:[UIView class]]
似乎是多余的?
 /* 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