xcode 如何判断 UITableView 是否包含特定的 NSIndexPath?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10506318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I tell if a UITableView contains a specific NSIndexPath?
提问by Ethan Allen
Here is the code I'm using:
这是我正在使用的代码:
if (appDelegate.currentMainIndexPath != nil /* && doesPathExistInTableView */)
{
[tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
appDelegate.currentMainIndexPath = nil;
}
采纳答案by HelmiB
You could try to get UITableViewCell
by :
你可以尝试UITableViewCell
通过:
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// returns nil if cell is not visible or index path is out of range
here's the complete code :
这是完整的代码:
UITableViewCell *cell = [cellForRowAtIndexPath:appDelegate.currentMainIndexPath];
if (appDelegate.currentMainIndexPath != nil && cell !=nil)
{
[tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
appDelegate.currentMainIndexPath = nil;
}
回答by Kamran Khan
You can use this. Pass it indexpath's row and section
你可以用这个。传递它 indexpath 的行和部分
Objective C:
目标 C:
-(BOOL) isRowPresentInTableView:(int)row withSection:(int)section
{
if(section < [self.tableView numberOfSections])
{
if(row < [self.tableView numberOfRowsInSection:section])
{
return YES;
}
}
return NO;
}
Swift 3:
斯威夫特 3:
func isRowPresentInTableView(indexPath: IndexPath) -> Bool{
if indexPath.section < tableView.numberOfSections{
if indexPath.row < tableView.numberOfRows(inSection: indexPath.section){
return true
}
}
return false
}
回答by Petter
A Swift adaptation of Kamran Khan's answer:
Kamran Khan 回答的 Swift 改编版:
extension UITableView {
func hasRowAtIndexPath(indexPath: NSIndexPath) -> Bool {
return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRowsInSection(indexPath.section)
}
}
Swift 4:
斯威夫特 4:
extension UITableView {
func hasRow(at indexPath: IndexPath) -> Bool {
return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
}
}
回答by MattZ
There is a more convenient method to tell if a indexPath is valid:
有一种更方便的方法来判断 indexPath 是否有效:
For Swift 3.0:
对于 Swift 3.0:
open func rectForRow(at indexPath: IndexPath) -> CGRect
For Objective-C
对于 Objective-C
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
You will get CGRectZero if the indexPath is invalid.
如果 indexPath 无效,您将获得 CGRectZero。
func isIndexPathValid(indexPath: IndexPath) -> Bool {
return !tableView.rectForRow(at: indexPath).equalTo(CGRect.zero)
}
回答by Titouan de Bailleul
If you mean, 'is there a cell at index n' then you just have to compare the size of you datasource to n
如果您的意思是,“索引 n 处是否有单元格”,那么您只需将数据源的大小与 n 进行比较
if (appDelegate.currentMainIndexPath != nil [datasource count] > n)
{
[tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
appDelegate.currentMainIndexPath = nil;
}
Where datasource is for instance an NSArray.
例如,数据源是一个 NSArray。
回答by Erik B
This is how you do it:
这是你如何做到的:
indexPath.row < [self.tableView numberOfRowsInSection:indexPath.section]
In context:
在上下文中:
if (indexPath.row < [self.tableView numberOfRowsInSection:indexPath.section]) {
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
Inserted into your code:
插入到您的代码中:
if (appDelegate.currentMainIndexPath != nil &&
indexPath.row < [tblView numberOfRowsInSection:indexPath.section]) {
[tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath
atScrollPosition:UITableViewScrollPositionTop
animated:NO];
appDelegate.currentMainIndexPath = nil;
}
回答by septerr
You can also use UITableView's numberOfRowsInSection
method.
你也可以使用 UITableView 的numberOfRowsInSection
方法。
回答by Stunner
I iterated upon Kamran's answer:
我重复了Kamran 的回答:
+ (BOOL)isIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView {
if (!indexPath) {
return NO;
}
if (indexPath.section < [tableView numberOfSections]) {
if (indexPath.row < [tableView numberOfRowsInSection:indexPath.section]) {
return YES;
}
}
return NO;
}