Categories: objective-c

【objective-c】UITableViewにあるセル全てをループして取得する方法

TableViewにTextFieldをセットしあり、それを全て取得したい状況ができた。

その時に全てのセルの1個1個を取得するにはどうすればいいだろうと調べた時のメモ。


以下のようにcellをループで回せる

NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
    for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
    {
        [cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
    }
}

上記ではcellsという変数に一度入れなおしてるが、別にfor文でそのまま回して処理しても構わない。

自分の作ったcellForRowAtIndexPathとかがもう1回呼ばれるのではとか思ったりしたけど、

そういうわけではなく、現在のcellを取得できる。

ちなみに今画面を表示しているセルを取得するには以下を用いる。

NSArray *cells = [tableView visibleCells];

これで表示しているセルだけを更新とかできるのでreloadDataよりもコスト少なめに画面更新ができる。

mogmet

View Comments

  • I may need your help. I've been doing research on gate io recently, and I've tried a lot of different things. Later, I read your article, and I think your way of writing has given me some innovative ideas, thank you very much.

  • Your article gave me a lot of inspiration, I hope you can explain your point of view in more detail, because I have some doubts, thank you.

Share
Published by
mogmet