簡體   English   中英

如果我們希望在同一表視圖控制器ios中使用靜態單元格以及動態單元格,那么如何在ios中實現呢?

[英]If we want static cell as well dynamic cells in same table view controller ios, how it can be possible in ios?

我有一個tableViewController,在此之下我想要一個靜態單元格,其余的將是動態單元格。 我已經為動態單元格運行,但是在同一個tableViewController中,我還需要添加一個靜態單元格,我該如何實現呢?

請幫忙

提前致謝。

您可以執行以下操作:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dynamicContent.count + 1; // +1 for the static cell at the beginning
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        // static cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StaticCellIdentifier" forIndexPath:indexPath];
        // customization
        return cell;
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DynamicCellIdentifier" forIndexPath:indexPath];
    id contentObject = self.dynamicContent[indexPath.row];
    // customization
    return cell;
}

您不能在UITableViewController中同時創建靜態和動態單元格。 但是,您可以對靜態單元格的數據進行硬編碼,並在每次重新加載表格視圖時加載數據。

您可以將所有單元格放在一個部分中,並繼續檢查index path.row == 0或為它們創建單獨的部分。

typedef NS_ENUM(NSUInteger, TableViewSectionType) {
    TableViewSectionType_Static,
    TableViewSectionType_Dynamic
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2; // One for static cell, and another for dynamic cells
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    switch(section) {
        case TableViewSectionType_Static:
            return 1; // Always return '1' to show the static cell at all times.
        case TableViewSectionType_Dynamic:
            return [myDynamicData count];
    }
}

通過這種方法,您的單元將分為兩部分,並且更易於管理。 並且它將始終顯示一個單元格,因為為TableViewSectionType_Static返回的行數始終為1。 它將根據您的數據計數顯示動態單元格。

暫無
暫無

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

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