How to subclass UITableView?

前端 未结 2 1868
南旧
南旧 2021-01-06 16:17

I honestly don\'t know how to subclass a UITableView. I\'m super confused and looking for whatever help I can get. How do I go about \"subclassing\" a UITableView? Reason I

相关标签:
2条回答
  • 2021-01-06 17:09

    Most of the time you shouldn't need to subclass UITableView, so see if you can avoid doing so first. If you absolutely have to, create a subclass that inherits from UITableView and override the touch-related methods in the implementation:

    // MyTableView.h
    
    @interface MyTableView : UITableView {
    }
    
    @end
    
    // MyTableView.m
    
    @implementation MyTableView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesMoved:touches withEvent:event];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesEnded:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
        [super touchesCancelled:touches withEvent:event];
    }
    
    @end
    
    0 讨论(0)
  • 2021-01-06 17:16

    In your .h file declare it as a subclass:

    @interface myViewController: UITableViewController
    

    Then implement the UITableViewController delegate methods:

    http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDelegate

    To detect touches on a cell write your logic in the tableView:didSelectRowAtIndexPath: method: http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:didSelectRowAtIndexPath:

    0 讨论(0)
提交回复
热议问题