如何获取每个单元格的位置
How to get each cell's position
我需要知道滚动时 tableView 中每个单元格的确切位置。
试过了,但它只是 returns 空..可能没有意义。
self.cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (self.cell == nil) {
self.cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSNumber *cellOriginY = [NSNumber numberWithFloat:self.cell.frame.origin.y];
[self.cellPositionArray addObject:cellOriginY];
}
我第一次尝试管理这个函数,我试图像这样用 indexPath 添加:
CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.view.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.tableView indexPathForRowAtPoint:visiblePoint];
哪种方法有效,但后来我发现选择了多个具有相同 indexPath 的单元格。我想这是因为 indexPath 不是唯一的,所以除了用每个单元格的 Y 轴管理它之外,我别无选择。
还有其他想法吗?
当我想尝试查看我的单元格如何自我格式化时,我通常会调用一个标签放在我的单元格上,以显示我需要的信息。我假设您使用的是自定义 UITableView
class,这就是为什么您调用 self.cell
而不是创建和 returning 单元格,就像您在所需的 UITableViewDataSourceProtocol
的。你没有你的函数头所以我真的不能告诉你,但是使用这个协议并确保你有 self.delegate = self.这应该会显示每个单元格以及您要查找的信息。如果您没有分组的 tableView,那么该部分每次都会显示 0。每个 IndexPath 都有两个值,section 和 row。
-(UITableViewCell *)cellForRowAtIndexPath:(UITableView *)tableView forIndexPath:(NSIndexPath *)indexPath {
if(self.cell == nil) {
UILabel *label = [UILabel new];
label.text = [NSString stringWithFormat:@"Cell %ld in section %ld starting at point %f", indexPath.row, indexPath.section, self.cell.fram.origin.y ];
[self.cell addSubView:label]; //Forgot to add this to first post.
//New Code For CustomViews
CustomView *customView = [self.customViews objectAtIndex:indexPath.row];
[self.cell addSubView:customView];
}
}
希望你能用它来找到你要找的东西。如果您有更多问题,请尝试在头文件中添加有关您正在执行此操作的 class 的更多信息。
//回答你的另一个问题:
我能想到的处理简单 tableView 的最佳方法是使用 NSArray。最简单的方法是将数组 属性 添加到 tableView class.
@property (strong, nonatomic, readwrite) NSMutableArray *customViews;
然后在 'ViewDidLoad' 中的 'ViewController.m' 初始化自定义 table 视图 class.
后初始化数组和其中的项目
'customTableView.customImages = [NSArray arrayWithObjects: cutomView1, customView2, etc..., nil];'
完成后,我将在我之前关于如何设置单元格视图的回答中添加代码。它将使第 0 行具有 objectAtIndex[0]、第 1 行 objectAtIndex[1] 等
确保更新您的 numberOfRowsInSection
协议方法
`return [self.customImages 计数]'
这样您的 table 视图就不会创建超出其需要的单元格。
试试这个,如果 cell.contentView
不起作用,可以在 cell.contentView
上使用任何控件。
CGPoint point = [cell.contentView.superview convertPoint:cell.contentView.frame.origin toView:nil];
用我已有的代码弄明白了。
这可能不是执行此操作的好方法,但.. 现在!
CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.view.bounds.size};
CGPoint zeroPoint = CGPointMake(CGRectGetMinX(visibleRect), CGRectGetMinY(visibleRect));
每次滚动时,检查 -
if (zeroPoint.y == (int)visibleIndexPath.row * 380)
380是每个单元格的高度。
虽然,它只有在您缓慢滚动时才有效,因此您必须增强 if 语句。
我需要知道滚动时 tableView 中每个单元格的确切位置。
试过了,但它只是 returns 空..可能没有意义。
self.cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (self.cell == nil) {
self.cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSNumber *cellOriginY = [NSNumber numberWithFloat:self.cell.frame.origin.y];
[self.cellPositionArray addObject:cellOriginY];
}
我第一次尝试管理这个函数,我试图像这样用 indexPath 添加:
CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.view.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.tableView indexPathForRowAtPoint:visiblePoint];
哪种方法有效,但后来我发现选择了多个具有相同 indexPath 的单元格。我想这是因为 indexPath 不是唯一的,所以除了用每个单元格的 Y 轴管理它之外,我别无选择。 还有其他想法吗?
当我想尝试查看我的单元格如何自我格式化时,我通常会调用一个标签放在我的单元格上,以显示我需要的信息。我假设您使用的是自定义 UITableView
class,这就是为什么您调用 self.cell
而不是创建和 returning 单元格,就像您在所需的 UITableViewDataSourceProtocol
的。你没有你的函数头所以我真的不能告诉你,但是使用这个协议并确保你有 self.delegate = self.这应该会显示每个单元格以及您要查找的信息。如果您没有分组的 tableView,那么该部分每次都会显示 0。每个 IndexPath 都有两个值,section 和 row。
-(UITableViewCell *)cellForRowAtIndexPath:(UITableView *)tableView forIndexPath:(NSIndexPath *)indexPath {
if(self.cell == nil) {
UILabel *label = [UILabel new];
label.text = [NSString stringWithFormat:@"Cell %ld in section %ld starting at point %f", indexPath.row, indexPath.section, self.cell.fram.origin.y ];
[self.cell addSubView:label]; //Forgot to add this to first post.
//New Code For CustomViews
CustomView *customView = [self.customViews objectAtIndex:indexPath.row];
[self.cell addSubView:customView];
}
}
希望你能用它来找到你要找的东西。如果您有更多问题,请尝试在头文件中添加有关您正在执行此操作的 class 的更多信息。 //回答你的另一个问题: 我能想到的处理简单 tableView 的最佳方法是使用 NSArray。最简单的方法是将数组 属性 添加到 tableView class.
@property (strong, nonatomic, readwrite) NSMutableArray *customViews;
然后在 'ViewDidLoad' 中的 'ViewController.m' 初始化自定义 table 视图 class.
后初始化数组和其中的项目'customTableView.customImages = [NSArray arrayWithObjects: cutomView1, customView2, etc..., nil];'
完成后,我将在我之前关于如何设置单元格视图的回答中添加代码。它将使第 0 行具有 objectAtIndex[0]、第 1 行 objectAtIndex[1] 等
确保更新您的 numberOfRowsInSection
协议方法
`return [self.customImages 计数]'
这样您的 table 视图就不会创建超出其需要的单元格。
试试这个,如果 cell.contentView
不起作用,可以在 cell.contentView
上使用任何控件。
CGPoint point = [cell.contentView.superview convertPoint:cell.contentView.frame.origin toView:nil];
用我已有的代码弄明白了。 这可能不是执行此操作的好方法,但.. 现在!
CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.view.bounds.size};
CGPoint zeroPoint = CGPointMake(CGRectGetMinX(visibleRect), CGRectGetMinY(visibleRect));
每次滚动时,检查 -
if (zeroPoint.y == (int)visibleIndexPath.row * 380)
380是每个单元格的高度。 虽然,它只有在您缓慢滚动时才有效,因此您必须增强 if 语句。