2010-01-05 309 views
1

我有一個UITableView在我正在刷新的iPhone應用程序中(通過在動作方法中調用[self.tableView reloadData]動態嵌入到其中一個UITableView單元中的UISegmentedControl。以更新其中一個單元格的文本值UITableView重新加載數據/刷新(可能的重複問題)

但是,下面的代碼似乎產生了一個不需要的副作用,看來每次UITableView刷新它時都會創建一個新的UISegmentedControl實例(可能還有圖像 - 我不確定)

我注意到這一點的唯一原因是,每次刷新一個裸露在UISegmentedControl周圍開始形成可感知的邊界,應用程序明顯減慢。我將非常感謝任何有關我目前困境的建議/代碼解決方案。

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSUInteger section = indexPath.section; 
    NSUInteger row = indexPath.row; 

    // Set up the cell...  


    //populates the personal info section 
    if (section == kPersonalInfoAddSection) { 

     if (row == kNameRow) { 

        //Other code irrelevant to this question was removed for the sake of clarity 
     } 
     else if(row == kHeightRow) { 

      cell.imageView.image = [UIImage imageNamed:@"tableview_height_label.png"]; 
        //THIS IS THE TEXT I'M TRYING TO UPDATE 
      cell.textLabel.text = [Formatter formatHeightValue:mainUser.heightInMM forZone:self.heightZone]; 
      cell.detailTextLabel.text = REQUIRED_STRING; 

     } 
    } 

    //populates the units section 
    if (section == kUnitsSection) { 

     if (row == kHeightUnitsRow) { 
      NSArray *heightUnitsSegments = [[NSArray alloc] initWithObjects:FT_AND_IN_STRING, M_AND_CM_STRING, nil]; 

      UISegmentedControl *heightUnitControl = [[UISegmentedControl alloc] initWithItems:heightUnitsSegments]; 

      CGRect segmentRect = CGRectMake(90, 7, 200, 30); 
      [heightUnitControl setFrame:segmentRect]; 
      //[heightUnitControl setSelectedSegmentIndex:0]; 
      [heightUnitControl addTarget:self action:@selector(heightSegmentClicked:) forControlEvents:UIControlEventValueChanged]; 
      heightUnitControl.tag = kHeightSegmentedControlTag; 

      cell.textLabel.text = @"Height:"; 
      cell.detailTextLabel.text = @"(units)"; 
      [cell.contentView addSubview:heightUnitControl]; 

      [heightUnitsSegments release]; 
      [heightUnitControl release]; 

     } 
     else if(row == kWeightUnitsRow) { 

        //Other code irrelevant to this question was removed for the sake of clarity  

     } 
    } 

    return cell; 
} 

謝謝大家提前!

回答

2

你是對的,它正在創建一個UISegmentedControl的新實例。這是因爲您正在使用通用的單元格標識符@「Cell」,然後每次都添加UISegmentedControl,從不刪除它。單元格獲取緩存包含UISegmentedControl,您檢索緩存的單元格並再次添加控件。

您可以使用更具體的細胞標識符,如果cell!= nil,您知道它已包含UISegmentedControl。或者每次你沒有使用已經包含控件的緩存單元時創建一個新單元。

使用圖像視圖,您只需設置單元格圖像視圖屬性,而無需向單元添加新視圖,以便一個都可以,每次都會被替換。

由於您嘗試更新的文本與UISegmentedControl無關,所以我認爲您應該可以使用更具體的單元標識符,並僅在單元格創建時添加控件。

+0

更好的是,創建一個自定義的UITableViewCell子類並使用它。如果您有更復雜的需求,沒有理由限制自己使用默認的單元格類型。 – TechZen 2010-01-05 20:08:07

+0

感謝您的回答。我只是想知道如何定製UITableViewCells在這種特殊情況下會有所幫助(我對iPhone開發相對較新)。 此外,似乎使用自定義單元格意味着分組視圖中單元格的圓角外觀消失(由方形conrners取代)。 – Urizen 2010-01-06 13:53:10

0
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSArray *arr1=[NSArray arrayWithObjects:@"img1.jpg",@"img2.jpg",nil]; 
    NSArray *arr2=[NSArray arrayWithObjects:@"img1.jpg",@"img2.jpg",@"img3.jpg",@"img4.jpg",@"img5.jpg",@"img6.jpg",nil]; 
    NSArray *arr3=[NSArray arrayWithObjects:@"img6.jpg",@"img5.jpg",@"img2.jpg",@"img1.jpg",nil]; 

    Imgs = [[NSArray alloc] initWithArray:[NSArray arrayWithObjects:arr1,arr2,arr3,nil]]; 


    NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:@"Ahmedabad",@"Name",@"Picture 5.png",@"Rating",@"Picture 1.png",@"Photo",arr1,@"img",nil]; 
    NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"Rajkot",@"Name",@"Picture 5.png",@"Rating",@"Picture 2.png",@"Photo",nil]; 
    NSDictionary *dic3=[NSDictionary dictionaryWithObjectsAndKeys:@"Baroda",@"Name",@"Picture 5.png",@"Rating",@"Picture 7.png",@"Photo",nil]; 

    tblArray=[[NSArray alloc] initWithObjects:dic1,dic2,dic3,nil]; 
    [myTbl reloadData]; 

} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    self.navigationController.navigationBarHidden=NO; 
    [self.navigationController.navigationBar setUserInteractionEnabled:YES]; 

} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    self.navigationController.navigationBarHidden=YES; 
} 



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [tblArray count]; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *CellIdentifer=[NSString stringWithFormat:@"%i",indexPath.row]; 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifer]; 
    if(cell==nil){ 
     cell=[self myCustomCell:CellIdentifer dicToSet:[tblArray objectAtIndex:indexPath.row]]; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    return cell; 
} 

-(UITableViewCell*)myCustomCell:(NSString*)CellIdentifer dicToSet:(NSDictionary*)dicToSet{ 
    UITableViewCell *cell=[[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 44) reuseIdentifier:CellIdentifer] autorelease]; 

    UIImageView *imgV=[[UIImageView alloc] initWithFrame:CGRectMake(2, 2, 40, 40)]; 
    [imgV setImage:[UIImage imageNamed:[dicToSet valueForKey:@"Photo"]]]; 
    [cell addSubview:imgV]; 
    [imgV release]; 

    UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(44, 2, 276, 20)]; 
    [lbl setText:[dicToSet valueForKey:@"Name"]]; 
    [cell addSubview:lbl]; 
    [lbl setBackgroundColor:[UIColor clearColor]]; 
    [lbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]]; 
    [lbl release]; 


    UIImageView *imgV1=[[UIImageView alloc] initWithFrame:CGRectMake(44, 24, 70, 20)]; 
    [imgV1 setImage:[UIImage imageNamed:[dicToSet valueForKey:@"Rating"]]]; 
    [cell addSubview:imgV1]; 
    [imgV1 release]; 


    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    nxtPlcDtl=[[plcFullDtl alloc] initWithNibName:@"plcFullDtl" bundle:nil]; 
    nxtPlcDtl.dict=[[NSDictionary alloc] initWithDictionary:[tblArray objectAtIndex:indexPath.row]]; 
    nxtPlcDtl.Imgs = [Imgs objectAtIndex:indexPath.row]; 
    nxtPlcDtl.comment1 = [comment1 objectAtIndex:indexPath.row]; 
    nxtPlcDtl.vedio = [vedio objectAtIndex:indexPath.row]; 

    [self.navigationController pushViewController:nxtPlcDtl animated:YES]; 



    } 
+0

我敢肯定,這是張貼在錯誤的線程。 – TechZen 2010-01-05 20:06:04