2012-08-09 42 views
0

我有一种情况,我已经将UILabelUITextView放置在同一位置的表格视图单元格中,它将交替放置。问题是当我滚动表视图时,标签和textview不匹配(重叠)。以下是cellForRowAtIndexPath的代码。UILabel和UITextView放置在表视图单元格在同一位置重复?

static NSString *CellIdentifier = @"CellIdentifier"; 
    // Dequeue or create a cell of the appropriate type. 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     //cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]]; 
     tableView.separatorColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"line_left_table.png"]]; 

     availableTtimeSlotLbl=[[UILabel alloc] initWithFrame:CGRectMake(10,10,200,40)]; 
     availableTtimeSlotLbl.backgroundColor=[UIColor clearColor]; 
     availableTtimeSlotLbl.textColor=[UIColor colorWithRed:17/255.0 green:61/255.0 blue:83/255.0 alpha:1]; 
     availableTtimeSlotLbl.font=[UIFont boldSystemFontOfSize:18]; 
     availableTtimeSlotLbl.tag=1; 
     [cell.contentView addSubview:availableTtimeSlotLbl]; 


     durationLbl=[[UILabel alloc] initWithFrame:CGRectMake(230,10, 250,45)]; 
     durationLbl.backgroundColor=[UIColor clearColor]; 
     durationLbl.textColor=[UIColor colorWithRed:17/255.0 green:61/255.0 blue:83/255.0 alpha:1]; 
     durationLbl.tag=2; 
     durationLbl.font=[UIFont systemFontOfSize:18]; 
     [cell.contentView addSubview:durationLbl]; 

     statusLbl=[[UILabel alloc] initWithFrame:CGRectMake(500,10,250,40)]; 
     statusLbl.backgroundColor=[UIColor clearColor]; 
     statusLbl.textColor=[UIColor colorWithRed:17/255.0 green:61/255.0 blue:83/255.0 alpha:1]; 
     statusLbl.tag=3; 
     statusLbl.font=[UIFont boldSystemFontOfSize:18]; 
     [cell.contentView addSubview:statusLbl]; 

     statusTxtVw=[[UITextView alloc] initWithFrame:CGRectMake(500,10,250,40)]; 
     statusTxtVw.backgroundColor=[UIColor clearColor]; 
     statusTxtVw.textColor=[UIColor colorWithRed:17/255.0 green:61/255.0 blue:83/255.0 alpha:1]; 
     statusTxtVw.tag=4; 
     statusTxtVw.font=[UIFont boldSystemFontOfSize:18]; 
     [cell.contentView addSubview:statusTxtVw]; 

     } 

    UILabel *slot=(UILabel *)[cell.contentView viewWithTag:1]; 
    NSString *slotStr= [[scheduleAppointmentAry objectAtIndex:indexPath.row]valueForKey:@"SlotTime"]; 
    slot.text=slotStr; 

    UILabel *duration=(UILabel *)[cell.contentView viewWithTag:2]; 
    NSString *durationStr= [[scheduleAppointmentAry objectAtIndex:indexPath.row]valueForKey:@"Station"]; 
    duration.text=durationStr; 

    NSString *statusStr= [[scheduleAppointmentAry objectAtIndex:indexPath.row]valueForKey:@"Status"]; 
    UILabel *status=(UILabel *)[cell.contentView viewWithTag:3]; 
    UITextView *statusTxtView = (UITextView *) [cell.contentView viewWithTag:4]; 

    if ([statusStr isEqualToString:@"Schedule"]) { 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
     statusTxtView.text = statusStr; 

    } 
    else if([statusStr isEqualToString:@"Unavailable"]) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     status.text=statusStr; 
    } 

    return cell; 

回答

0

行的内容重复出现,因为单元格正在重新使用。您需要适当地重置它们。

if ([statusStr isEqualToString:@"Schedule"]) { 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    statusTxtView.text = statusStr; 
    // ADDED: Reset status label 
    status.text = nil; 
} 
else if([statusStr isEqualToString:@"Unavailable"]) { 

    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    status.text=statusStr; 
    // ADDED: Reset status text view 
    statusTxtView.text = nil; 
} 
相关问题