【问题标题】:UITableView reloading data / refreshing (possible duplication issue)UITableView 重新加载数据/刷新(可能重复问题)
【发布时间】:2011-01-01 17:13:30
【问题描述】:

我在一个我正在刷新的 iPhone 应用程序中有一个 UITableView(通过在操作方法中调用 [self.tableView reloadData] 来动态嵌入一个 UITableView 单元格中的 UISegmentedControl。刷新表格视图以更新文本其中一个单元格的值。

但是,以下代码似乎会产生不希望的副作用。似乎每次 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;
}

提前谢谢大家!

【问题讨论】:

    标签: iphone objective-c uitableview reload


    【解决方案1】:

    你是对的,它正在创建 UISegmentedControl 的新实例。这是因为您使用的是通用单元格标识符 @"Cell",然后每次都添加 UISegmentedControl,从不删除它。包含 UISegmentedControl 的单元格被缓存,您检索缓存的单元格并再次添加控件。

    您可以使用更具体的单元格标识符,如果 cell != nil 您知道它已经包含 UISegmentedControl。或者每次创建一个新单元格,这样您就不会使用已包含控件的缓存单元格。

    使用图像视图,您只需设置单元格图像视图属性,而不向单元格添加新视图,这样就可以了,每次都会被替换。

    由于您尝试更新的文本与 UISegmentedControl 无关,我认为您应该能够使用更具体的单元格标识符并仅在单元格创建时添加控件。

    【讨论】:

    • 更好的是,创建一个自定义 UITableViewCell 子类并使用它。如果您有更复杂的需求,没有理由将自己限制在默认的单元格类型中。
    • 感谢您的回答。我只是想知道自定义 UITableViewCells 如何在这种特殊情况下提供帮助(我对 iPhone 开发比较陌生)。此外,似乎使用自定义单元格意味着分组视图中单元格的圆形外观消失(由方形 conrners 代替)。
    【解决方案2】:
    - (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];
    
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多