繁体   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