UITableView和UITableViewCell在不同iOS版本的層級關係

來源:互聯網
上載者:User

標籤:

  這是我人生中的第一篇部落格,興奮,激動,哈哈!!!

  UITableView時iOS中非常常用的一個控制項,一般情況下那幾個必須實現的代理方法就夠我們實現一些簡單的介面了。但有時候猛然遇到一個關於tableview的問題,也會令像我這種的小白不知所措。

- (NSIndexPath *)GetIndexPath:(id)sender

{

      UIButton        *btn = ((UIButton *)sender);

        UITableViewCell *cell = (UITableViewCell *)[[btn superview] superview];

        return [self.tableView indexPathForCell:cell];

}

   sender是cell上添加的一個控制項,那麼為什麼要兩個superview呢,因為在cell和它的子控制項中間其實還有一層contentView。所以準確的說cell的子控制項應該是contentView,而contentView的子控制項才是sender。這是iOS7之前的情況,iOS7以後,在cell和contentView之間又多了一個scrollView,所以上邊那句代碼應該改成   UITableViewCell *cell = (UITableViewCell*)[[[btn superview] superview] superview];

  這樣在iOS7和iOS8.0好像都可以,但後面8.3就又不行了,至於為什麼,我現在也沒有弄清楚(哪位大神知道的話,記得告訴我一下哈)。但是我在網上發現了另一種擷取cell的方法。

- (NSIndexPath *)GetIndexPath:(id)sender

{

    UIView *view = sender;

    while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {

        view = [view superview];

    }

    UITableView *cell = (UITableView *)view;

    return [self.tableView indexPathForCell:cell];

}

  實用於任意層的層級關係,哈哈,確實霸氣。

  UITableViewCell的superview得到UItableView,這是iOS7以前的,7以後中間也多了一層UITableViewWrapperView。

  第一次部落格,略顯簡短、粗糙,歡迎大家指教

UITableView和UITableViewCell在不同iOS版本的層級關係

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.