UITableView单元格附件的保存状态? [英] Saving state of UITableView cell accessory?
问题描述
我在表格视图中设置了手势识别器.
I have gesture recognisers set up on my table view.
- 向右滑动,附件将变成刻度线图像
- 向左滑动即可更改为人字形图像
如果点击一个单元格,它将加载本地HTML文件.
If a cell is tapped, it loads a local HTML file.
如果向右滑动,将显示刻度线.但是,如果您随后点击一个单元格以查看HTML文件并返回到表格视图,则图像将还原为人字形.
If you swipe to the right, the tick appears as it should. However, if you then tap a cell to view a HTML file and come back to the table view, the image reverts to the chevron.
确保刻度线保持应有的最佳方式是什么?
What's the best way to ensure the tick stays as it should?
更多代码:
来自"viewDidLoad":
From 'viewDidLoad':
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSwipeRight:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.tableView addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSwipeLeft:)];
//recognizer.delegate = self;
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.tableView addGestureRecognizer:recognizer];
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
//Get the cell out of the table view
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Update the cell or model
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"disclosure.png"]];
}
}
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
if(indexPath)
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"MFGCell";
MFGCell *cell = (MFGCell *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MFGCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.itemTitle.text = [item objectAtIndex:indexPath.row];
cell.itemDescription.text = [description objectAtIndex:indexPath.row];
cell.itemImageView.image = [UIImage imageNamed:[icons objectAtIndex:indexPath.row]];
return cell;
}
推荐答案
为响应用户的滑动,您应该存储用户的选择(例如,存储在 NSMutableArray
类型的私有实例变量中).当用户返回到表格视图时,您可以重新使用 tableView:cellForRowAtIndexPath:
中的信息,以使用正确的附件样式设置单元格.
In reaction to the user's swipe you should store the user's choice (e.g. in a private instance variable of type NSMutableArray
). When the user comes back to the table view you can then reuse the information in your tableView:cellForRowAtIndexPath:
to setup the cell with the correct accessory style.
财产声明:
@property(nonatomic, retain) NSMutableArray* _accessoryStyle;
合成属性.然后将此代码段添加到 handleSwipeLeft:
的底部以存储用户的选择:
Synthesize the property. Then add this snippet to the bottom of handleSwipeLeft:
to store the user's choice:
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
[...]
NSNumber* number = [numberWithInt:0];
[_accessoryStyle replaceObjectAtIndex:indexPath.row withObject:number];
}
在 handleSwipeRight:
:
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
[...]
NSNumber* number = [numberWithInt:1];
[_accessoryStyle replaceObjectAtIndex:indexPath.row withObject:number];
}
在 tableView:cellForRowAtIndexPath:
:
NSString* accessoryImageName;
NSNumber* number = [_accessoryStyle objectAtIndex:indexPath.row];
switch ([number intValue])
{
case 0:
accessoryImageName = @"disclosure.png";
break;
case 1:
accessoryImageName = @"tick.png";
break;
default:
// replace with your error handling code
return nil;
}
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:accessoryImageName]];
要使所有这些工作正常进行,您需要使用希望表视图具有单元格的元素数量相同的元素来初始化 _accessoryStyle
数组.例如,在您的视图控制器的 viewDidLoad
:
For all this to work you need to initialize the _accessoryStyle
array with the same number of elements that you expect your table view to have cells. For instance, in your view controller's viewDidLoad
:
- (void) viewDidLoad
{
[super viewDidLoad];
self._accessoryStyle = [NSMutableArray arrayWithCapacity:0];
NSNumber* defaultAccessoryStyle = [numberWithInt:0];
int numberOfRows = 17; // get the real number from somewhere
for (int index = 0; index < numberOfCells; ++index)
[_accessoryStyle addObject:defaultAccessoryStyle];
}
要平衡这一点,您需要添加
And to balance this you need to add
- (void) viewDidUnload
{
[super viewDidUnload];
self._accessoryStyle = nil;
}
还有很多改进的余地:
- 找到更好的变量名
- 对不同样式使用枚举,而不仅仅是硬编码数字0和1
- 不要为每个表视图单元格分配新的
UIImageView
,只需分配其中两个,并根据附件样式使用正确的一个即可.
- Find better variable names
- Use an enumeration for the different styles instead of just hardcoded numbers 0 and 1
- Do not allocate a new
UIImageView
for each table view cell, just allocate two of them and use the right one depending on the accessory style
这篇关于UITableView单元格附件的保存状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!