32

複数の行を持つUITableViewがあるとしましょう。特定の時点ですべてのUITableViewCellをNSArrayとして取得したいと思います。私が試してみました

[tableView visibleCells] 

ただし、このアプローチには問題があります。現在の画面に表示されていないセルをすべて持つことはできません。そこで私は別のアプローチに目を向けました:

-(NSArray *)cellsForTableView:(UITableView *)tableView
{
    NSInteger sections = tableView.numberOfSections;
    NSMutableArray *cells = [[NSMutableArray alloc]  init];
    for (int section = 0; section < sections; section++) {
        NSInteger rows =  [tableView numberOfRowsInSection:section];
        for (int row = 0; row < rows; row++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];   // **here, for those cells not in current screen, cell is nil**
            [cells addObject:cell];
        }
    }
    return cells;
}

しかし、このアプローチでは、画面に表示されていないセルを取得できないようです。誰かがこれについて私を助けることができますか?

4

9 に答える 9

22

tableViewがすべてのセルを格納しているわけではないという理由だけで、それが可能だとは思いません。再利用のために、表示されていない一部のセルのみを保存するキャッシュメカニズムを使用します。

なぜすべてのセルが必要なのですか?多分あなたはあなたがやろうとしていることを達成することができます、そうでなければ。

于 2012-10-23T10:28:35.510 に答える
15

これがSwift3.0の最も簡単なソリューションです

func getAllCells() -> [UITableViewCell] {

    var cells = [UITableViewCell]()
    // assuming tableView is your self.tableView defined somewhere
    for i in 0...tableView.numberOfSections-1
    {
        for j in 0...tableView.numberOfRowsInSection(i)-1
        {
            if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i)) {

               cells.append(cell)
            }

        }
    }
 return cells
 }
于 2015-09-21T06:32:47.603 に答える
7

セルを作成するたびに、必要なときにいつでも解析できるNSMutableArrayにセルを追加できます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *Cell = [self dequeueReusableCellWithIdentifier:@"Custom_Cell_Id"];
    if (Cell == NULL)
    {
        Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Custom_Cell_Id"]];
        [Cells_Array addObject:Cell];
    }
}

- (void) DoSomething
{
    for (int i = 0;i < [Cells count];i++)
    {
        CustomCell *Cell = [Cells objectAtIndex:i];
        //Access cell components
    }
}
于 2014-01-31T09:54:48.000 に答える
2

ただし、現在のセルを取得するためのより簡単な実装は、Set ie O(1)を使用することです。

/// Reference to all cells.
private var allCells = Set<UITableViewCell>()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Your_Cell_ID") as! UITableViewCell
    if !allCells.contains(cell) { allCells.insert(cell) }
    return cell
}
于 2017-03-21T03:02:13.500 に答える
2

私はこの問題のトリックを見つけたと思います。これを試して ;)

self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.contentSize.width, self.tableView.contentSize.height);

UITableViewはのcellForRowAtIndexPath高さに応じて呼び出すtableView.frameため、tableViewの高さを更新する必要があります。

于 2018-07-18T16:21:34.313 に答える
1

の実装方法を誤解しているのではないかと思いますUITableView。理解しておくべき重要な点は、見えないセルは実際には存在しない(または少なくとも存在しない可能性がある)ということです。

スクロールすると、で 使用されUITableViewているため、古いセルが新しいセルに置き換えられます 。dequeueReusableCellWithIdentifier

-(UITableViewCell) cellForRowAtIndexPath (NSIndexPath *)indexPath
于 2012-10-23T10:29:00.857 に答える
1

機能ソリューションswift5.0

extension UITableView {
    /**
     * Returns all cells in a table
     * ## Examples:
     * tableView.cells // array of cells in a tableview
     */
    public var cells: [UITableViewCell] {
      (0..<self.numberOfSections).indices.map { (sectionIndex: Int) -> [UITableViewCell] in
          (0..<self.numberOfRows(inSection: sectionIndex)).indices.compactMap { (rowIndex: Int) -> UITableViewCell? in
              self.cellForRow(at: IndexPath(row: rowIndex, section: sectionIndex))
          }
      }.flatMap { $0 }
    }
}
于 2018-05-22T11:42:34.717 に答える
0

それらのテキストフィールドの値が必要ですか?はいの場合、indexpath.rowを使用してテキストフィールドのタグプロパティを介してアクセスできます

于 2012-10-23T10:38:31.470 に答える
0

SWIFT 5 UITableViewはセルを再利用します。つまり、100個のアイテムをUITableViewにロードできますが、一度に表示できるのは10個のアイテムのみです。ユーザーが別のアイテムを表示したい場合は、スクロールする必要があります。したがって、メモリ最適化UITableView では、13個のセルのみがロードされます(10個のセルが表示され、3個のセルがプリロードされます)。表示されているすべてのセル(この例では10個のセル)を取得する場合は、次を使用できます。

let visibleCells = yourTableView.visiblecells

ただし、ロードされたすべてのセル(この例では13セル)を取得する場合は、次の拡張機能を使用することをお勧めします。

extension UITableView {

func getAllCells() -> [UITableViewCell] {
    var cells = [UITableViewCell]()
    for i in 0..<self.numberOfSections
    {
        for j in 0..<self.numberOfRows(inSection: i)
        {
            if let cell = self.cellForRow(at: IndexPath(row: j, section: i)) {
                cells.append(cell)
            }
            
        }
    }
    return cells
}

}

次に、次の呼び出しによってすべてのセルを取得できます。

let allLoadedCells = yourtableView.getAllCells()
于 2021-10-11T16:03:49.890 に答える