14

ユーザーがスクロールするときに、UITableView 内の特定の行の位置を修正できるようにしたいと考えています。

具体的には、特定の行が後続の行の「ヘッダー」であるテーブルがあり、ユーザーが上にスクロールしてもヘッダーが画面の上部に留まるようにします。その後、ユーザーがスクロールして次のヘッダー行に移動すると、邪魔にならないように移動します。

同様の例は、Any.DO アプリです。「今日」、「明日」、「後で」の表の行は、常に画面に表示されます。

これをどのように実装できるかについて誰か提案がありますか?

私は現在、TableDidScroll デリゲートに従い、自分のセルをテーブル ビューの前の適切な場所に配置することを考えています。問題は、これらのセルを実際の表のセルにして、たとえばユーザーが並べ替えられるようにしたい場合があることです。

ありがとう、

ティム

4

3 に答える 3

13

私はこれで遊んでいて、簡単な解決策を思いつきました。

まず、単一の UITableViewCell プロパティをコントローラーに追加します。これは、偽のセクション ヘッダーを作成するために使用する行セルとまったく同じように初期化する必要があります。

次に、テーブル ビューのスクロールをインターセプトします。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Add some logic here to determine the section header. For example, use 
    // indexPathsForVisibleRows to get the visible index paths, from which you 
    // should be able to get the table view row that corresponds to the current 
    // section header. How this works will be implementation dependent.
    //
    // If the current section header has changed since the pervious scroll request 
    // (because a new one should now be at the top of the screen) then you should
    // update the contents.

    IndexPath *indexPathOfCurrentHeaderCell = ... // Depends on implementation
    UITableViewCell *headerCell = [self.tableView cellForRowAtIndexPath:indexPathOfCurrentHeaderCell];

    // If it exists then it's on screen. Hide our false header

    if (headerCell)
        self.cellHeader.hidden = true;

    // If it doesn't exist (not on screen) or if it's partially scrolled off the top,
    // position our false header at the top of the screen

    if (!headerCell || headerCell.frame.origin.y < self.tableView.contentOffset.y )
    {
        self.cellHeader.hidden = NO;
        self.cellHeader.frame = CGRectMake(0, self.tableView.contentOffset.y, self.cellHeader.frame.size.width, self.cellHeader.frame.size.height);
    }

    // Make sure it's on top of all other cells

    [self.tableView bringSubviewToFront:self.cellHeader];
}

最後に、そのセルでのアクションをインターセプトし、正しいことを行う必要があります...

于 2012-06-12T13:29:42.693 に答える
3

UITableViewこれは、プレーンインスタンスのセクション ヘッダーのデフォルトの動作です。カスタム ヘッダーを作成する場合tableView:viewForHeaderInSection:は、テーブル ビュー デリゲートにメソッドを実装し、ヘッダーのビューを返します。

ただし、行だけでなく、セクションと行を管理する必要があります。

于 2012-06-05T09:21:22.770 に答える
0

Swift 5 ソリューション

var header: UIView?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(indexPath: indexPath) as UITableViewCell
    header = cell.contentView
    return cell
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let headerCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0))
    guard headerCell == nil || (headerCell!.frame.origin.y < self.tableView.contentOffset.y + headerCell!.frame.height/2) else {
        header?.isHidden = true
        return
    }
    guard let hdr = header else { return }
    hdr.isHidden = false
    hdr.frame = CGRect(x: 0, y: tableView.contentOffset.y, width: hdr.frame.size.width, height: hdr.frame.size.height)
    if !tableView.subviews.contains(hdr) {
        tableView.addSubview(hdr)
    }
    tableView.bringSubviewToFront(hdr)
}
于 2019-06-25T14:47:50.263 に答える