重新加载部分处理不当 [英] Reload Section does not handle properly
问题描述
我有以下实现,其中我有 title
和 tablecell 上的两个按钮(标题是 half
和 full
).当用户选择并关闭该部分并再次打开它时,他只会看到按钮标题的默认值 full
而不是他的选择.
I have the following implementation where I have title
, and two buttons(titles are half
and full
) on the tablecell. When user selects and close the section and open it again, he only sees the default values of button title which is full
rather than his selection.
我可以看到以下方法 (setHalfButton :indexPath
) 在重新加载期间被调用,但它没有任何效果.
I could able to see the following method (setHalfButton :indexPath
) is getting called during the reloading, but it does not have any effect.
代码和截图如下.
- (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]];
}
推荐答案
您的 setHalfButton
和 setFullButton
函数正在从 cellForRowAtIndexPath:
调用datasource 方法,但他们正在调用 cellForRowAtIndexPath:
tableview 方法.由于您还没有从前一种方法返回单元格,后一种方法会将 nil
返回给 cell
,从而导致没有可见的更新.
Your setHalfButton
and setFullButton
functions are being called from the cellForRowAtIndexPath:
datasource method, but they are calling the cellForRowAtIndexPath:
tableview method. Since you have not yet returned the cell from the former method, the latter method will return nil
to cell
, resulting in no visible update.
setHalfButton
和 setFullButton
方法应该在你的 ComboTableViewCell
类中:
The setHalfButton
and setFullButton
methods should be in your ComboTableViewCell
class:
-(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
类中,并使用委托模式通知视图控制器半/全已更改.
Also, you are adding the button action handlers each time you dequeue a cell, but you should only do this when you allocate a new cell. From a design point-of-view these button tap handlers should also be in your ComboTableViewCell
class with a delegation pattern to notify the view controller that the half/full was changed.
至少它应该是这样的:
-(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;
}
这篇关于重新加载部分处理不当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!