2012-08-09 51 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; 
} 
相關問題