2013-09-23 28 views
9

Tôi nhận được UITableViewCell một UIButton thuộc như thế này:Bắt UITableViewCell với SuperView trong iOS 7

-(void)buttonHandler:(UIButton *)button { 

    OrderCell *cell = [[button superview] superview]; 
    NSLog(@"cell.item = %@", cell.item.text); 

Và nó hoạt động tốt trong bất cứ điều gì trước khi iOS 7. Tuy nhiên, mang lại cho tôi:

[UITableViewCellScrollView item]: chọn không được công nhận gửi đến dụ 0x17ae2cf0

nếu tôi chạy các ứng dụng trong iOS 7. NHƯNG nếu tôi làm:

-(void)buttonHandler:(UIButton *)button { 

    OrderCell *cell = [[[button superview] superview] superview]; 
    NSLog(@"cell.item = %@", cell.item.text); 

Sau đó, nó hoạt động trong iOS 7 nhưng không phải trước đó?!?!?!

Tôi đang phá vỡ vấn đề này bằng cách làm này:

OrderCell *cell; 
if([[[UIDevice currentDevice] systemVersion] isEqualToString:@"7.0"]) 
    cell = [[[button superview] superview] superview]; 
else 
    cell = [[button superview] superview]; 

NSLog(@"cell.item = %@", cell.item.text); 

nhưng WTF đang xảy ra !? Có ai biết tại sao điều này xảy ra không?

Cảm ơn!

+2

Mã của bạn đã được tùy thuộc vào cấu trúc subview tin của 'UITableViewCell '. Rõ ràng là cấu trúc đã thay đổi trong iOS 7. Có nhiều cách an toàn hơn để làm những gì bạn muốn. Và mã mới của bạn sẽ bị hỏng dưới iOS 7.1 và iOS 8. – rmaddy

+0

@rmaddy Cách an toàn hơn là gì? – Mundi

+1

Bạn có thể lặp qua giám sát, kiểm tra xem chúng có thuộc loại UITableViewCell và sau đó trả lại chế độ xem đó không? Xem câu trả lời: – CW0007007

Trả lời

27

Một giải pháp tốt hơn là thêm một loại cho UIView (SuperView), và gọi đó là bởi:

UITableViewCell *cell = [button findSuperViewWithClass:[UITableViewCell class]] 

Bằng cách này, mã của bạn làm việc cho tất cả các phiên bản tương lai và iOS qua

@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 
+9

Mã này có thể được cải thiện bằng cách thêm 'ngắt' khi bạn tìm thấy trình giám sát phù hợp. Không cần phải đi tất cả các con đường lên đến cửa sổ mỗi lần. – rmaddy

-2
if ([[button superView] isKindOfClass:[UITableViewCell class]]) { 

} 

else //check next : 
13

Cách tốt nhất để làm điều này là:

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; 
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; 
UITableViewCell *cell = (UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath]; 
+0

Câu trả lời hay nhất, quyết định rất đáng tin cậy – zest

3

Để hoàn thành câu trả lời của @ thomas-keuleers này là phương pháp nhanh chóng:

extension UIView { 

    func findSuperViewWithClass<T>(superViewClass : T.Type) -> UIView? { 

     var xsuperView : UIView! = self.superview! 
     var foundSuperView : UIView! 

     while (xsuperView != nil && foundSuperView == nil) { 

      if xsuperView.self is T { 
       foundSuperView = xsuperView 
      } else { 
       xsuperView = xsuperView.superview 
      } 
     } 
     return foundSuperView 
    } 

} 

và bạn chỉ cần gọi như thế:

child.findSuperViewWithClass(TableViewCell) 
+0

child.findSuperViewWithClass (TableViewCell.self) – sasquatch

Các vấn đề liên quan