簡體   English   中英

顯示數據的可擴展UITableView單元格

[英]Expandable UITableView cells showing datas

我有一些按年份分類的運動比賽,每場比賽都有最終結果,比賽日期和得分手。

我在“表格視圖”中顯示這些匹配項,如下所示:

在此處輸入圖片說明

所以我想要實現的是:單擊一個單元格時,顯示如圖所示的比賽詳細信息。

我還找到了一些庫來實現手風琴/可擴展樣式,但是沒有人能做到。 他們只是擴大單元格並顯示另一個單元格。

在這種情況下,您甚至不需要使用擴展/手風琴。 這是解決該問題的方法。 假設正常情況下的單元格大小為40,特別單擊時的單元格大小為100。在heightForRowAtIndexPath中,您可以檢查選擇了哪個單元格並返回更高的高度

if(selectedRow == indexPath.row) {
    return 100;
} else {
    return 40;
}

然后,您可以在didSelectRowAtIndexPath或clickEvent方法中進行操作

[self.tableView beginUpdates];
[[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: selectedRow inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

因此,它將是您的單元格全部呈現的內容,但是根據高度,您將隱藏或顯示內容。

使用輸入源更新答案

ViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(selectedIndex == indexPath.row)
    return 100;
else
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    customCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSDictionary *matches = self.dataArray[indexPath.row];
    cell.matchName.text = [matches objectForKey@"MatchName"];
    if() {
        cell.heightConstraintSecondView.constant = 0;
    } else {
        cell.heightConstraintSecondView.constant = 59;
        cell.team1.text = [matches objectForKey@"Team1"];
        cell.team2.text = [matches objectForKey@"Team2"];
        cell.score.text = [matches objectForKey@"Score"];
    } 
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selectedIndex = indexPath.row;
    [self.tableView beginUpdates];
    [[self tableView] reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    }
}

較干凈的方法是創建2個具有不同標識符的單元格,然后在運行時單擊該單元格進行更改。 檢查以下問題: iOS中的UITableView中的下拉列表

您可以為此目的使用HVtableview,請嘗試此鏈接https://github.com/xerxes235/HVTableView

嘗試以下方法之一:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM