【问题标题】:UITableView Cell is regeneratingUITableView Cell 正在重新生成
【发布时间】:2011-08-12 08:35:39
【问题描述】:

我有UITableViewCellUILabelUISwitch。默认情况下,所有UISwitch 都设置为关闭。

一旦我打开开关,然后滚动表格,开关值将再次设置为默认值,即关闭

下面是我使用的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell != nil) cell = nil;
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if (indexPath.row == 0) {
        UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
        lbl1.text = @"Some Text";
        [cell addSubview:lbl1];

        switch = [[UISwitch alloc] initWithFrame:CGRectMake(190, 10, 200, 30)];
        [switch setOn:NO];
        switch.tag = 1;
        [switch addTarget:self action:@selector(switchTapped:) forControlEvents:UIControlEventChanged];
        [cell addSubview:switch];
    }

}

// 下面是我的switchTapped方法:

- (void) switchTapped: (id)sender {
    UISwitch *tapSwitch = (UISwitch *)sender;

    switch (tapSwitch.tag) {
        case 1:
            if (tapSwitch.on) {
              // do something
            }
            else {
                // do something
            }
            break;
        case 2:
            if (tapSwitch.on) {
                // do something
            }
            else {
                // do something
            }
            break;
}

我在这里做错了吗?

谢谢。

【问题讨论】:

    标签: iphone objective-c uitableview uiswitch


    【解决方案1】:

    您正在使用非常讨厌的代码,每次需要时都会重新生成单元格:

    if(cell != nil) 
    {
       cell = nil; 
    }
    
    if (cell == nil) 
    { 
    ...
    }
    

    您是否将开关的状态绑定到某个保留对象(例如,项目模型对象,其中单个单元格反映了一个项目)?

    【讨论】:

    • 如果我不使用它,标签会被覆盖
    • 那么你就有答案了 :) 尝试将你的开关状态绑定到一个保留的对象,当需要一个单元格时,只需重用它。
    • 这里是:你的表必须有一个数据源,对吧?所以,如果你有它,它就是对象的集合(NSArray、NSMutableArray 等)。每个对象都可以有一些可以绑定到表格单元格的保留属性。例如:假设您将单个单元格绑定到的对象具有 2 个属性:NSString* 描述和 BOOL isChecked。然后,无论何时您需要一个给定索引的单元格(假设为 0),您都必须引用您的 collection[0] 项目并将单元格的标签文本分配给该项目的描述。开关 (isChecked) 也是如此。
    【解决方案2】:

    这就是我编写单元格绘图代码的方式:

    基本上,在我的 if(cell == nil) { ... } 中,我做了所有的“initWithFrame”。除此之外,我只需设置标签文本等值。不要在 if(cell == nil) {...} 代码块之外执行 initWithFrame。

    -(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
    {
        static NSString *reusableCell = @"reusableCell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableCell];
    
        if(cell == nil)
        {        
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableCell] autorelease];
    
            thumbnail = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 106, 81)];
    
            feedTitle = [[UILabel alloc] initWithFrame:CGRectMake(116, 3, [IOSDevice screenWidth] - 140, 25)];
            postDate = [[UILabel alloc] initWithFrame:CGRectMake(116, 10, [IOSDevice screenWidth] - 140, 50)];
            description = [[UILabel alloc] initWithFrame:CGRectMake(116, 45, [IOSDevice screenWidth] - 140, 50)];
    
            // setting tag reminders so we can identify the element to replace
            [thumbnail setTag:1];
            [feedTitle setTag:2];
            [postDate setTag:3];
            [description setTag:4];
    
            [[cell contentView] addSubview:thumbnail];
            [[cell contentView] addSubview:feedTitle];
            [[cell contentView] addSubview:description];
            [[cell contentView] addSubview:postDate];
    
            [thumbnail release];
            [feedTitle release];
            [description release];
            [postDate release];
        }
    
        thumbnail = (UIImageView *)[[cell contentView] viewWithTag:1];
        feedTitle = (UILabel *)[[cell contentView] viewWithTag:2];
        postDate = (UILabel *)[[cell contentView] viewWithTag:3];
        description = (UILabel *)[[cell contentView] viewWithTag:4];
    
        [feedTitle setBackgroundColor:[UIColor clearColor]];
        [feedTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
        [feedTitle setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];
    
        [description setBackgroundColor:[UIColor clearColor]];
        [description setFont:[UIFont fontWithName:@"Helvetica" size:12]];
        [description setTextColor:[UIColor colorWithRed:0.328 green:0.328 blue:0.328 alpha:1.0]];
        [description setNumberOfLines:2];
        [description setLineBreakMode:UILineBreakModeWordWrap];
    
        [postDate setBackgroundColor:[UIColor clearColor]];
        [postDate setFont:[UIFont fontWithName:@"Helvetica" size:12]];
        [postDate setTextColor:[UIColor colorWithRed:0.707 green:0.180 blue:0.141 alpha:1.0]];
    
        [thumbnail setImage:[[items objectAtIndex:[indexPath row]] objectForKey:@"thumb"]];
    
        [feedTitle setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"title"]];
        [description setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"summary"]];
    
        // Format date
        NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
        [dateFormatter setDateStyle:NSDateFormatterLongStyle];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    
        [postDate setText:[dateFormatter stringFromDate:[[items objectAtIndex:[indexPath row]] objectForKey:@"date"]]];
    
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
        if([feedList contentOffset].y < -50)
        {
            shouldUpdate = TRUE;
    
            [activityIndicator stopAnimating];
    
            [feedList setContentOffset:CGPointMake(0, -30) animated:NO];
            [self loadData];
    
            loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -25, [IOSDevice screenWidth], 20)];
            [loadingLabel setText:@"Loading New Data"];
            [loadingLabel setTextAlignment:UITextAlignmentCenter];
            [loadingLabel setBackgroundColor:[UIColor clearColor]];
            [loadingLabel setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];
            [loadingLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
    
            reloadingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(70, -25, 20, 20)];
            [reloadingSpinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
            [reloadingSpinner startAnimating];
            [reloadingSpinner setHidesWhenStopped:YES];
    
            [feedList addSubview:reloadingSpinner];
            [feedList addSubview:loadingLabel];
        }
    
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2011-11-28
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多