亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何知道UITableView行號

如何知道UITableView行號

小怪獸愛吃肉 2019-07-05 13:39:57
如何知道UITableView行號我有一個UITableViewCell帶著UISwitch作為每個單元格的輔助視圖。當我在單元格中更改開關的值時,如何知道開關在哪一行?我需要切換值更改事件中的行號。
查看完整描述

3 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

如果您設置tag屬性設置為行號(如其他答案所建議的),您必須在tableView:cellForRowAtIndexPath:(因為可以對不同的行重用單元格)。

相反,當您需要行號時,您可以沿著superview鏈子UISwitch(或任何其他視圖)UITableViewCell,然后到UITableView,并詢問單元格的索引路徑的表視圖:

static NSIndexPath *indexPathForView(UIView *view) {
    while (view && ![view isKindOfClass:[UITableViewCell class]])
        view = view.superview;
    if (!view)
        return nil;
    UITableViewCell *cell = (UITableViewCell *)view;
    while (view && ![view isKindOfClass:[UITableView class]])
        view = view.superview;
    if (!view)
        return nil;
    UITableView *tableView = (UITableView *)view;
    return [tableView indexPathForCell:cell];}

這不需要在tableView:cellForRowAtIndexPath:.


查看完整回答
反對 回復 2019-07-05
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

接受的解決方案是一種聰明的攻擊。

但是,如果我們可以利用已經可用的,為什么我們需要使用hitpoint呢?tag財產上UIView?你會說標記只能存儲行或節。因為它是單曲INT.

好吧.。別忘了你的根人(CS 101)。單曲INT可以存儲兩個兩倍小的整數。這里有一個擴展:

extension Int {

    public init(indexPath: IndexPath) {
        var marshalledInt: UInt32 = 0xffffffff

        let rowPiece = UInt16(indexPath.row)
        let sectionPiece = UInt16(indexPath.section)
        marshalledInt = marshalledInt & (UInt32(rowPiece) << 16)
        marshalledInt = marshalledInt + UInt32(sectionPiece)

        self.init(bitPattern: UInt(marshalledInt))
    }

    var indexPathRepresentation: IndexPath {
        let section = self & 0x0000ffff

        let pattern: UInt32 = 0xffff0000
        let row = (UInt32(self) & pattern) >> 16
        return IndexPath(row: Int(row), section: Int(section))
    }}

在你的tableView(_:, cellForRowAt:)然后你可以:

cell.yourSwitch.tag = Int(indexPath: indexPath)

然后在操作處理程序中可以:

func didToogle(sender: UISwitch){
    print(sender.tag.indexPathRepresentation)}

不過,請注意它的局限性:行和區段不需要大于65535。(UInt 16.max)

我懷疑你的tableView的指數會那么高,但如果它們是這樣的話,挑戰你自己,并實施更有效的包裝方案。假設我們有一個很小的部分,我們不需要所有的16位來表示一個節。我們的int布局如下:

{section area length}{all remaining}[4 BITS: section area length - 1]

那是我們的4LSBs指定區段區域-1的長度,因為我們至少為一個區段分配了1位。因此,在我們的部分為0的情況下,行最多可以占用27位([1][27][4]),這絕對是足夠的。


查看完整回答
反對 回復 2019-07-05
  • 3 回答
  • 0 關注
  • 516 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號