我在我的表格 View 上设置了手势识别器。
- 向右滑动,附件变为勾号图像
- 向左滑动,变成人字形图片
如果点击一个单元格,它会加载一个本地 HTML 文件。
如果您向右滑动,勾号会按原样出现。但是,如果您随后点击一个单元格以查看 HTML 文件并返回到表格 View ,图像将恢复为人字形。
确保滴答声保持其应有状态的最佳方法是什么?
编辑
更多代码:
来自“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
类型的私有(private)实例变量中)。当用户返回到表格 View 时,您可以重新使用 tableView:cellForRowAtIndexPath:
中的信息,以使用正确的附件样式设置单元格。
属性声明:
@property(nonatomic, retain) NSMutableArray* _accessoryStyle;
综合属性。然后将此代码段添加到 handleSwipeLeft:
的底部以存储用户的选择:
- (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]];
要使所有这些工作正常,您需要使用与您希望表格 View 具有单元格的元素数量相同的元素来初始化 _accessoryStyle
数组。例如,在您的 View Controller 的 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];
}
为了平衡这一点,您需要添加
- (void) viewDidUnload
{
[super viewDidUnload];
self._accessoryStyle = nil;
}
还有很大的改进空间:
- 找到更好的变量名
- 对不同的样式使用枚举,而不仅仅是硬编码数字 0 和 1
- 不要为每个表格 View 单元格分配一个新的
UIImageView
,只分配其中两个并根据附件样式使用正确的一个
关于objective-c - 保存 UITableView 单元格附件的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14073819/