如果我们想在同一个表视图控制器ios中使用静态单元格以及动态单元格,那么它在ios中是如何实现的?

时间:2021-12-24 08:16:38

I have a tableViewController, and under that i want one static cell and rest of all will be dynamic cells . 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,在那之下我想要一个静态单元格,其余部分将是动态单元格。我已经运行了动态单元格,但在同一个tableViewController中我还需要添加一个静态单元格,我该如何实现呢?

Please Help

Thanks in advance.

提前致谢。

3 个解决方案

#1


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

#2


You cannot create static and dynamic cell at same time in a UITableViewController. But you can hard code your static cell's data and load the data each time you reload your tableview.

您无法在UITableViewController中同时创建静态和动态单元格。但是,每次重新加载tableview时,您都可以对静态单元格的数据进行硬编码并加载数据。

#3


You can keep all your cells in one section and keep checking for index path.row == 0 or create separate sections for them.

您可以将所有单元格保留在一个部分中,并继续检查索引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. It will show the dynamic cells based on your data count.

通过这种方法,您的单元格将分为两个部分,并且更易于管理。并且它将始终显示一个单元格,因为TableViewSectionType_Static返回的行数始终为1。它将根据您的数据计数显示动态单元格。

#1


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

#2


You cannot create static and dynamic cell at same time in a UITableViewController. But you can hard code your static cell's data and load the data each time you reload your tableview.

您无法在UITableViewController中同时创建静态和动态单元格。但是,每次重新加载tableview时,您都可以对静态单元格的数据进行硬编码并加载数据。

#3


You can keep all your cells in one section and keep checking for index path.row == 0 or create separate sections for them.

您可以将所有单元格保留在一个部分中,并继续检查索引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. It will show the dynamic cells based on your data count.

通过这种方法,您的单元格将分为两个部分,并且更易于管理。并且它将始终显示一个单元格,因为TableViewSectionType_Static返回的行数始终为1。它将根据您的数据计数显示动态单元格。