iOS 點選UITableViewCell上的控制元件獲取對應的cell
場景
很多情況下cell上有很多可以觸發的子控制元件,例如點贊,提示,選擇等操作,介紹幾個方法,隨意感受下
方法1 (獲取SuperView)
@implementation UIView (GetCellFromContentviewSubview)
- (UITableViewCell *)getCellFromContentviewSubview
{
if ([[[self superview] superview] isKindOfClass:[UITableViewCell class]]) {
return (UITableViewCell *)[[self superview] superview];
}
else if ([[[[self superview] superview] superview] isKindOfClass:[UITableViewCell class]]) {
return (UITableViewCell *)[[[self superview] superview] superview];
}
else{
NSLog(@"Something Panic Happens");
}
return nil;
}
@end
方法2 (另一種方式獲取SuperView)
@interface UIView (SuperView)
- (UIView *)findSuperViewWithClass:(Class)superViewClass;
@end
@implementation UIView (SuperView)
- (UIView *)findSuperViewWithClass:(Class)superViewClass {
UIView *superView = self.superview;
UIView *foundSuperView = nil;
while (nil != superView && nil == foundSuperView) {
if ([superView isKindOfClass:superViewClass]) {
foundSuperView = superView;
} else {
superView = superView.superview;
}
}
return foundSuperView;
}
@end
// 呼叫
UITableViewCell *cell = [button findSuperViewWithClass:[UITableViewCell class]]
方法3 (牆裂推薦)
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
UITableViewCell *cell = (UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
這個方法看上去比上面兩個舒服多了吧,簡單理解下
第一段:指定的控制元件A,A區域裡面的有個座標(0,0)的位置,把該座標相對於A控制元件的位置轉換到相對於tableView的座標
第二段:indexPathForRowAtPoint
根據轉換的座標,獲取對應的indexpath
第三段:根據Indexpath獲取對應的cell
相關文章
- 點選Cell控制UITableViewCell的展開及關閉UIView
- iOS cell找對應的tableViewiOSView
- (iOS)點選cell推出現新介面iOS
- iOS 中tableview cell點選取消選中效果iOSView
- iOS cell上的定時器iOS定時器
- 獲取UITableViewCell中UITextField的值方法總結UIView
- iOS:改變UITableViewCell的選中背景色iOSUIView
- [WPF]獲取控制元件間的相對位置控制元件
- UITableViewCell的高度快取UIView快取
- iOS不規則控制元件的點選事件iOS控制元件事件
- 新版POI如何獲取日期型別的cell的值型別
- iOS UITableViewCell的動畫效果(一)iOSUIView動畫
- 更改select下拉選單項獲取對應的值和文字
- 點選事件獲取當前li的索引事件索引
- WPF中TreeView控制元件如何獲取選中的TreeViewItem物件?View控制元件物件
- C#如何獲取ListView控制元件選中項的值C#View控制元件
- iOS 中 cell的用法iOS
- jQuery獲取點選td單元格的值jQuery
- 【iOS開發】解決UITableview中選中Cell後,Cell複用導致選中狀態消失、應用閃退的問題iOSUIView
- MUI TableViewCell 上按鈕點選Cell同時觸發解決UIView
- iOS 重寫cell的FrameiOS
- iOS-UITableViewCell的一些事iOSUIView
- iOS開發之有間距的UITableViewCelliOSUIView
- iOS全埋點解決方案-控制元件點選事件iOS控制元件事件
- iOS cell中webview自適應高度iOSWebView
- jQuery點選元素獲取此元素的id屬性值jQuery
- JavaScript獲取當前點選元素的id屬性值JavaScript
- 點選按鈕獲取當前位置
- iOS UITableViewCell允許滑動的處理iOSUIView
- cookie 中通過 鍵 獲取相對應的 值Cookie
- FileUpload 上傳的檔案獲取相對路徑
- [急⚠] BasePage 如何封裝獲取 elements 的第 x 個的點選?封裝
- 【Javascript】獲取選中的文字JavaScript
- Listview獲取選中行的值View
- 自定義UIView UITableViewCell等控制元件UIView控制元件
- 對應定義結構獲取
- iOS 應用下載連結獲取iOS
- iOS 自定義tableView Cell、高度自適應iOSView