[英]UItableview indexPathForCell - iOS6 'v' iOS7
我創建了一個帶有嵌入式分段控件的自定義UItableviewcell,單擊該分段控件時,我有一個需要行索引路徑的方法。 使用iOS7,以下方法運行良好,我獲得了索引路徑,然后可以更新自定義單元格中的其他值。
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
不幸的是,當我使用iOS6進行測試時,indexPath設置為NULL,現在它在iOS6和iOS7上的工作方式是否有所不同?
做一些研究后,我沒有這樣做是最好的方法,而不是使用超級視圖,我現在搜索視圖層次結構。
UIView *view = sender;
while (![view isKindOfClass:[UITableViewCell class]]) {
view = [view superview];
}
現在,這對於iOS6和7均適用。我遇到的另一個問題是,分段控件的默認高度對於iOS7是不同的,並且會影響行高。
您可以創建UISegmentedControl的子類並添加indexPath屬性,然后在cellForRowAtIndexPath中設置indexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SegmentCell *returnCell = [tableView dequeueReusableCellWithIdentifier:@"SegmentCell" forIndexPath:indexPath];
returnCell.segmentedControl.indexPath = indexPath;
return returnCell;
}
更清晰,而且如果視圖層次結構發生變化,您的解決方案也不會中斷。
這是我認為的最好方法:
CGPoint subviewPosition = [發送方convertPoint:CGPointZero toView:self.myTable];
NSIndexPath * indexPath = [self.myTable indexPathForRowAtPoint:subviewPosition];
無論是iOS6還是7,它都將返回正確的索引路徑
這是因為與iOS6不同,在iOS7中,UITableViewWrapperView是UITableViewCell的超級視圖
你應該用
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
//iOS7
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
} else {
//iOS6
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
}
感謝null( iOS7中的UITableViewCell indexPath崩潰 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.