【问题标题】:Reload Section does not handle properly重新加载部分无法正确处理
【发布时间】:2018-01-28 05:57:24
【问题描述】:

我有以下实现,我有title,表格单元上有两个按钮(标题是halffull)。当用户选择并关闭该部分并再次打开它时,他只看到按钮标题的默认值full 而不是他的选择。

我可以看到以下方法(setHalfButton :indexPath ) 在重新加载期间被调用,但它没有任何效果。

代码和截图如下。

- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath setQuantity :(double) quantity  {
    NSMutableArray* array = [selectedRowsInSectionDictionary objectForKey:@(indexPath.section)];
    if(array){
        [array addObject:indexPath];
    } else {
        array = [NSMutableArray array];
        [array addObject:indexPath];
        [selectedRowsInSectionDictionary setObject:array forKey:@(indexPath.section)];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{          
    static NSString *cellIdentifier = @"ComboCell";
    ComboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[ComboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    if([selectedRowsInSectionDictionary[@(indexPath.section)] containsObject: indexPath])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        if(
       comboItemsArray[indexPath.section].allComboItems[indexPath.row].pQuantity == 0.5)
         {
            // it comes here after reloading
             [self setHalfButton:indexPath];
         }
         else
         {
             [self setFullButton:indexPath];
         }
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.halfBtnOutlet.hidden = YES;
        cell.fullBtnOutlet.hidden = YES;
    }
    cell.comboTitle.text =comboItemsArray[indexPath.section].allComboItems[indexPath.row].pName;

    [cell.halfBtnOutlet addTarget:self action:@selector(halfBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.fullBtnOutlet addTarget:self action:@selector(fullBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void) setHalfButton : (NSIndexPath*)indexPath
{
    ComboTableViewCell* cell = [comboTableView cellForRowAtIndexPath:indexPath];
    [cell.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
    [cell.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
    [cell.halfBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
    [cell.fullBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}

-(void) setFullButton : (NSIndexPath*)indexPath
{
    ComboTableViewCell* cell = [comboTableView cellForRowAtIndexPath:indexPath];
    [cell.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
    [cell.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
    [cell.fullBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
    [cell.halfBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}

【问题讨论】:

  • 你好 Paulw11,请把它作为答案,然后我会标记它。

标签: objective-c xcode uitableview


【解决方案1】:

您的 setHalfButtonsetFullButton 函数正在从 cellForRowAtIndexPath: 数据源方法调用,但它们正在调用 cellForRowAtIndexPath: tableview 方法。由于您还没有从前一种方法返回单元格,后一种方法将返回nilcell,导致没有可见的更新。

setHalfButtonsetFullButton 方法应该在您的 ComboTableViewCell 类中:

-(void) setHalfButton 
{
    [self.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
    [self.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
    [self.halfBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
    [self.fullBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}

-(void) setFullButton
{
    [self.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
    [self.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
    [self.fullBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
    [self.halfBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}

此外,每次将单元格出列时,您都会添加按钮操作处理程序,但仅应在分配新单元格时执行此操作。从设计的角度来看,这些按钮点击处理程序也应该在您的 ComboTableViewCell 类中,并使用委托模式通知视图控制器半/全已更改。

至少它应该看起来像这样:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{          
    static NSString *cellIdentifier = @"ComboCell";
    ComboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[ComboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell.halfBtnOutlet addTarget:self action:@selector(halfBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.fullBtnOutlet addTarget:self action:@selector(fullBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    if([selectedRowsInSectionDictionary[@(indexPath.section)] containsObject: indexPath])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        if(
       comboItemsArray[indexPath.section].allComboItems[indexPath.row].pQuantity == 0.5)
         {
            // it comes here after reloading
             [cell setHalfButton];
         }
         else
         {
             [cell setFullButton];
         }
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.halfBtnOutlet.hidden = YES;
        cell.fullBtnOutlet.hidden = YES;
    }
    cell.comboTitle.text =comboItemsArray[indexPath.section].allComboItems[indexPath.row].pName;


    return cell;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 2012-05-25
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
相关资源
最近更新 更多