셀에서 UITableViewCell indexPath를 얻는 방법은 무엇입니까?
어떻게 셀에서, 그는 어떻게해야합니까 indexPath
A의 UITableView
?
스택 오버플로와 Google을 검색했지만 모든 정보는 그 반대입니다. superView
/ 에 액세스 한 UITableView
다음 셀을 검색하는 방법이 있습니까?
컨텍스트에 대한 추가 정보 : 두 개의 클래스가 있습니다. 하나는 호출 Cue
되고 하나는 호출됩니다 CueTableCell
(의 하위 클래스 UITableViewCell
) CueTableCell
는 시각적 표현입니다 Cue
(두 클래스 모두 서로에 대한 포인터가 있음). Cue
객체는 연결 목록에 있으며 사용자가 특정 명령을 수행 할 때 다음 항목의 시각적 표현 ( CueTableCell
)을 Cue
선택해야합니다. 이렇게 Cue
클래스가 호출 select
다음에 방법 Cue
를 검색하는 목록 UITableView
으로부터 cell
그 호출을 selectRowAtIndexPath:animated:scrollPosition:
,하는 것이 요구 indexPath
의를 UITableViewCell
.
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
논란의 여지가있는 것으로 간주되는 경우에도 UITableView 문서를 읽는 데 도움이됩니다 (아래 주석 참조).
셀은 인덱스 경로가 무엇인지 알지 못합니다. 컨트롤러를 조작하는 UI 요소가 데이터 모델 또는 UI 상호 작용에 기초하는 수정 된 데이터에 기초하여 임의의 코드를 포함한다. ( MVC 참조 .)
UITableViewCell에서 이것을 시도하십시오.
NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self];
편집 : iOS 8.1.2 이상에서는 작동하지 않는 것 같습니다. 지적 해 주신 Chris Prince에게 감사드립니다.
이 간단한 팁을 시도해 볼 수 있습니다.
온 UITableViewCell
클래스 :
@property NSInteger myCellIndex; //or add directly your indexPath
그리고 당신의 cellForRowAtIndexPath
방법에 :
...
[cell setMyCellIndex : indexPath.row]
이제 어디서나 셀 인덱스를 검색 할 수 있습니다.
다음과 같이 셀의 .h 파일에 약한 tableView 속성을 넣으십시오.
@property (weak,nonatomic)UITableView *tableView;
다음과 같이 cellForRowAtIndex 메서드에 속성을 할당합니다.
cell.tableView = tableView;
이제 어디에서든 indexPath of cell이 필요합니다.
NSIndexPath *indexPath = [cell.tableView indexPathForCell:cell];
"이것은 나쁜 생각입니다" 라고 말하는 사람들을 다루기 위해 , 제 경우에는이 버튼을 UITableViewCell
눌렀을 때 다른 뷰로 이동 하는 버튼이 있어야합니다 . 이것은 셀 자체에 대한 선택이 아니기 때문에 [self.tableView indexPathForSelectedRow]
작동하지 않습니다.
이로 인해 두 가지 옵션이 남습니다.
- 뷰에 전달해야하는 개체를 테이블 셀 자체에 저장합니다. 이 작업을 것이지만, 그것은을 가진 내 지점을 물리 칠 것
NSFetchedResultsController
내가 있기 때문에 하지 않는 메모리에 모두에게 개체를 저장할 테이블이 긴 경우에는 특히. - 인덱스 경로를 사용하여 페치 컨트롤러에서 항목을 검색합니다. 예,
NSIndexPath
해킹으로 알아 내야한다는 것은 추악한 것 같지만 궁극적으로 메모리에 개체를 저장하는 것보다 비용이 저렴합니다.
indexPathForCell:
사용하기에 올바른 방법이지만 여기에 방법이 있습니다 (이 코드는의 하위 클래스에서 구현 된 것으로 가정합니다 UITableViewCell
.
// uses the indexPathForCell to return the indexPath for itself
- (NSIndexPath *)getIndexPath {
return [[self getTableView] indexPathForCell:self];
}
// retrieve the table view from self
- (UITableView *)getTableView {
// get the superview of this class, note the camel-case V to differentiate
// from the class' superview property.
UIView *superView = self.superview;
/*
check to see that *superView != nil* (if it is then we've walked up the
entire chain of views without finding a UITableView object) and whether
the superView is a UITableView.
*/
while (superView && ![superView isKindOfClass:[UITableView class]]) {
superView = superView.superview;
}
// if superView != nil, then it means we found the UITableView that contains
// the cell.
if (superView) {
// cast the object and return
return (UITableView *)superView;
}
// we did not find any UITableView
return nil;
}
추신 : 내 실제 코드는 테이블보기에서이 모든 것을 액세스하지만, 누군가가 테이블 셀에서 직접 이와 같은 작업을 수행하려는 이유에 대한 예를 제공하고 있습니다.
신속한
let indexPath :NSIndexPath = (self.superview.superview as! UITableView).indexPathForCell(self)!
이 질문에 대한 답은 실제로 저에게 많은 도움이되었습니다.
나는 사용했다 NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
제 sender
경우 UITableViewCell
에는 prepareForSegue
방법 에서 비롯됩니다 .
내가 가지고 있지 않았기 때문에 이것을 사용 TableViewController
했지만UITableView property outlet
의 제목을 찾아야 Cell
했기 때문에 indexPath를 알아야했습니다.
이것이 누구에게나 도움이되기를 바랍니다!
iOS 11.0에서는 다음을 사용할 수 있습니다 UITableView.indexPathForRow(at point: CGPoint) -> IndexPath?
.
if let indexPath = tableView.indexPathForRow(at: cell.center) {
tableView.selectRow
(at: indexPath, animated: true, scrollPosition: .middle)
}
셀의 중심점을 얻은 다음 tableView는 해당 지점에 해당하는 indexPath를 반환합니다.
try this (it only works if the tableView has only one section and all cells has equal heights) :
//get current cell rectangle relative to its superview
CGRect r = [self convertRect:self.frame toView:self.superview];
//get cell index
int index = r.origin.y / r.size.height;
Try with this from your UITableViewCell:
NSIndexPath *indexPath = [(UITableView *)self.superview.superview indexPathForCell:self];
Swift 4.x
To provide access to my cell, i did something like this:
I have an extension of the UIView:
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
And then.. in my cell Class:
class SomeCell: UITableViewCell {
func someMethod() {
if let myViewController = self.parentViewController as? CarteleraTableViewController {
let indexPath : IndexPath = (myViewController.tableView).indexPath(for: self)!
print(indexPath)
}
}
}
Thats all form me.
Best regards.
참고URL : https://stackoverflow.com/questions/15711889/how-to-get-uitableviewcell-indexpath-from-the-cell
'program tip' 카테고리의 다른 글
데이터 파일에서 ASCII가 아닌 문자 제거 (0) | 2020.11.04 |
---|---|
파이썬은 Ruby에서 || =와 같은 "또는 같음"함수를 가지고 있습니까? (0) | 2020.11.03 |
파이썬 목록의 모든 요소에 논리 연산자를 적용하는 방법 (0) | 2020.11.03 |
MySQL에서 누적 합계 열 만들기 (0) | 2020.11.03 |
장고 비밀번호 생성기 (0) | 2020.11.03 |