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

I have a tableViewController, and under that i want one static cell and rest of all will be dynamic cells . 我有一个tableViewController,在此之下我想要一个静态单元格,其余的将是动态单元格。 I have already run for dynamic cells , but within same tableViewController i also need to add one static cell, how can i achieve it? 我已经为动态单元格运行,但是在同一个tableViewController中,我还需要添加一个静态单元格,我该如何实现呢?

Please Help 请帮忙

Thanks in advance. 提前致谢。

you could do something like the following: 您可以执行以下操作:

- (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;
}

You cannot create static and dynamic cell at same time in a UITableViewController. 您不能在UITableViewController中同时创建静态和动态单元格。 But you can hard code your static cell's data and load the data each time you reload your tableview. 但是,您可以对静态单元格的数据进行硬编码,并在每次重新加载表格视图时加载数据。

You can keep all your cells in one section and keep checking for index path.row == 0 or create separate sections for them. 您可以将所有单元格放在一个部分中,并继续检查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];
    }
}

With this approach your cells will be split into two sections and it will be easier to manage. 通过这种方法,您的单元将分为两部分,并且更易于管理。 And it will always show one cell, as number of rows returned for TableViewSectionType_Static is 1 always. 并且它将始终显示一个单元格,因为为TableViewSectionType_Static返回的行数始终为1。 It will show the dynamic cells based on your data count. 它将根据您的数据计数显示动态单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM