ios 在表格视图中结合静态和原型内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9322885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:53:58  来源:igfitidea点击:

Combine static and prototype content in a table view

iphoneiosuitableviewstoryboard

提问by tazboy

Is there a way to combine static tableview cells (static content) with dynamic tableview cells (prototype content) using storyboard?

有没有办法使用故事板将静态 tableview 单元格(静态内容)与动态 tableview 单元格(原型内容)结合起来?

回答by danh

I suggest you treat your table as dynamic, but include the cells you always want at the top. In the Storyboard, place a UITableViewControllerand have it use a dynamic table. Add as many UITableViewCellprototypes to the table as you need. Say, one each for your static cells, and one to represent the variable cells.

我建议您将表格视为动态表格,但在顶部包含您一直想要的单元格。在 Storyboard 中,放置一个UITableViewController并让它使用一个动态表。根据UITableViewCell需要向表中添加任意数量的原型。比如说,每个静态单元格一个,一个代表可变单元格。

In your UITableViewDataSourceclass:

在你的UITableViewDataSource课堂上:

#define NUMBER_OF_STATIC_CELLS  3

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dynamicModel count] + NUMBER_OF_STATIC_CELLS;
}

and, then

进而

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row < NUMBER_OF_STATIC_CELLS) {
        // dequeue and configure my static cell for indexPath.row
        NSString *cellIdentifier = ... // id for one of my static cells
    } else {
        // normal dynamic logic here
        NSString *cellIdentifier = @"DynamicCellID"
        // dequeue and configure for [self.myDynamicModel objectAtIndex:indexPath.row]
    }
}

回答by AbuZubair

I had a problem, although it was a slight variant of this. I actually wanted to mix dynamic and static cells but in different groups. Meaning group 1 would have static only cells and group 2 would have dynamic cells.

我有一个问题,虽然它是这个的一个轻微的变体。我实际上想混合动态和静态单元格,但在不同的组中。这意味着组 1 将只有静态单元格,组 2 将具有动态单元格。

I accomplished this by actually hard coding static cell values (based on their prototype cell identifiers). The dynamic sections would have normal dynamically populated content. Here is some example code in case anyone else has the same issue:

我通过实际硬编码静态单元格值(基于它们的原型单元格标识符)来实现这一点。动态部分将具有正常的动态填充内容。这是一些示例代码,以防其他人遇到同样的问题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{    
    if (section == 1){
        return @"Dynamic Cells";
    }
    if (section == 0){
        return @"Static Cells";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1; //However many static cells you want
    } else {
        return [_yourArray count];
    }
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.section == 0) {
        NSString *cellIdentifier = @"staticCellType";   
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        cell.textLabel.text = @"some static content";        
        return cell;

    } else if (indexPath.section == 1){

        NSString *cellIdentifier = @"dynamicCellType";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        cell.textLabel.text = [_yourArray objectAtIndex:indexPath.row];
        return cell;

    } 
    return nil;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

回答by Sean

Since no one has actually provided a real answer to the problem (using both static and prototype cells in the same table view), I figured I'd chime in. It can be done!

由于没有人真正为这个问题提供真正的答案(在同一个表视图中同时使用静态和原型单元格),我想我会插话。它可以做到!

Create your static cells as you see fit. For the sections that need a dynamic cell, if you are NOT using standard UITableViewCell type, you'll need to create your custom one in a separate Nib, otherwise you can use the standard ones. Then implement the following delegates. Basically for each of these delegates, for the static stuff we want to call super, for the dynamic, we return our values.

根据需要创建静态单元格。对于需要动态单元格的部分,如果您不使用标准 UITableViewCell 类型,则需要在单独的 Nib 中创建自定义类型,否则您可以使用标准类型。然后实现以下委托。基本上对于这些委托中的每一个,对于我们想要调用 super 的静态东西,对于动态,我们返回我们的值。

First, IF you need to selectively show your dynamic section, you'll want to implement the numberOfSectionsInTableView (otherwise you can leave this delegate out):

首先,如果您需要有选择地显示您的动态部分,您将需要实现 numberOfSectionsInTableView (否则您可以忽略此委托):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    int staticSections = 1;
    int dynamicSections = 1;
    if (SOME_BOOLEAN) {
        return staticSections + dynamicSections;
    } else {
        return staticSections;
    }
}

Then, you need to implement numberOfRowsInSection:

然后,您需要实现 numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return A_COUNT;
    } else {
        return [super tableView:tableView numberOfRowsInSection:section];
    }
}

Then, you need to implement heightForRowAtIndexPath:

然后,您需要实现 heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 44.0f;
    } else {
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
}

Then indentationLevelForRowAtIndexPath:

然后缩进LevelForRowAtIndexPath:

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 1; // or manually set in IB (Storyboard)
    } else {
        return [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath]; // or 0
    }
}

Finally, cellForRowAtIndexPath:

最后,cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        SomeObject *obj = self.someArray[indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DynamicCell" forIndexPath:indexPath];
        cell.textLabel.text = obj.textValue;
        return cell;
    } else {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}

回答by lnafziger

Unfortunately, this is not possible since static table views mustbe in a UITableViewController and that only allows one tableview.

不幸的是,这是不可能的,因为静态表视图必须在 UITableViewController 中并且只允许一个表视图。

What you need to do is make three more dynamic UITableviewCell's and load them individually for the first three rows where you want the static content.

您需要做的是制作三个更动态的 UITableviewCell,并为您想要静态内容的前三行分别加载它们。

If you aren't sure how to do this, let me know and I can find some code.

如果您不确定如何执行此操作,请告诉我,我可以找到一些代码。

回答by Michael

You can always make one you your tableviews appear similar to the static table but define it in code. Set the sections, amount or rows per section, headers etc. through the delegate methods.

你总是可以让你的 tableview 看起来类似于静态表,但在代码中定义它。通过委托方法设置节、每节的数量或行数、标题等。

回答by T.J.

You can't have one tableview be static and the other dynamic in the same view controller so you will need to make them both dynamic. In the first tableview you will configure the cells in code on initializing the view controller never update them.

您不能在同一个视图控制器中让一个 tableview 是静态的,而另一个是动态的,因此您需要使它们都是动态的。在第一个 tableview 中,您将在初始化视图控制器时在代码中配置单元格,永远不会更新它们。

  1. Add a UIViewController to your storyboard.
  2. Add two Table Views (Not TableViewControllers) to the UIView Controller.
  3. Select each tableView and configure both for dynamic cells.
  4. Build and attach your view controller. 2 tableview on a single viewexplains that step.
  1. 将 UIViewController 添加到您的故事板。
  2. 向 UIView 控制器添加两个表视图(不是 TableViewController)。
  3. 选择每个 tableView 并为动态单元格配置两者。
  4. 构建并附加您的视图控制器。单个视图上的 2 个 tableview解释了该步骤。

As another option you can achieve a similar look by embedding your dynamic tableview in part of a view similar to the link in step 4 and then do whatever you wanted to in the rest of the view to setup what you were planning to do with static cells by using scrollviews, labels, and buttons.

作为另一种选择,您可以通过将动态 tableview 嵌入到类似于步骤 4 中的链接的视图的一部分中来实现类似的外观,然后在视图的其余部分中执行您想要的任何操作来设置您计划对静态单元格执行的操作通过使用滚动视图、标签和按钮。

回答by BeckProduct

You could also create buttons (one for each static cell you have) that are styled like your cells and place them in the tableHeaderView or tableFooterView of the UITableView; those buttons are just views after all.

您还可以创建与您的单元格样式相似的按钮(每个静态单元格一个),并将它们放置在 UITableView 的 tableHeaderView 或 tableFooterView 中;毕竟这些按钮只是视图。

You'll need to add some logic for making selections on the buttons vs. the cells so it maintains the usual look and feel.

您需要添加一些逻辑来在按钮和单元格上进行选择,以保持通常的外观和感觉。

Of course, this assumes that you want to insert static cells into your table view at the top or bottom of the table.

当然,这假设您想将静态单元格插入到表格顶部或底部的表格视图中。

回答by Pierre Bernard

One way to have dynamic content in a static table view is to clone cells where additional rows are needed.

在静态表视图中拥有动态内容的一种方法是克隆需要额外行的单元格。

For the dynamic section of my table view, I lay out one or more cells in Interface Builder. At runtime, I can clone those by archiving using NSCoder and then unarchiving.

对于表格视图的动态部分,我在 Interface Builder 中布置了一个或多个单元格。在运行时,我可以通过使用 NSCoder 归档然后取消归档来克隆它们。

It works, but is not necessarily prettier than starting with a dynamic prototype table view and creating static rows from there.

它有效,但不一定比从动态原型表视图开始并从那里创建静态行更漂亮。

It fails with standard table view cells. The lazily created text labels are not laid out correctly. Hence I used UITableViewCell subclasses where I take care of archiving and unarchiving subviews.

标准表格视图单元格失败。懒惰创建的文本标签布局不正确。因此,我使用 UITableViewCell 子类来处理归档和取消归档子视图。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == kContactsSection) {
        NSArray     *contacts   = self.contacts;
        Contact     *contact    = [contacts objectAtIndex:indexPath.row];
        NSString    *name       = contact.name;
        NSString    *role       = contact.role;

        if ([role length] == 0) {
            NNContactDefaultTableViewCell *cell = (id)[tableView dequeueReusableCellWithIdentifier : @"contactDefault"];

            if (cell == nil) {
                NNContactDefaultTableViewCell   *template   = (id)[super tableView : tableView
                                                         cellForRowAtIndexPath :[NSIndexPath indexPathForRow:0 inSection:kContactsSection]];

                NSData                          *data       = [NSKeyedArchiver archivedDataWithRootObject:template];

                cell = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            }

            cell.contactTextLabel.text = name;

            return cell;
        }
        else {
            NNContactDetailTableViewCell *cell = (id)[tableView dequeueReusableCellWithIdentifier : @"contactDetail"];

            if (cell == nil) {
                NNContactDetailTableViewCell    *template   = (id)[super tableView : tableView
                                                        cellForRowAtIndexPath :[NSIndexPath indexPathForRow:1 inSection:kContactsSection]];

                NSData                          *data       = [NSKeyedArchiver archivedDataWithRootObject:template];

                cell = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            }

            cell.contactTextLabel.text          = name;
            cell.contactDetailTextLabel.text    = role;

            return cell;
        }
    }

    return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}

In the above example I have two cell types. Both laid out in Interface Builder as part of a static table view.

在上面的例子中,我有两种细胞类型。两者都在 Interface Builder 中作为静态表视图的一部分进行布局。

To get dynamic content in one section, I also need to override the following methods:

要在一个部分中获取动态内容,我还需要覆盖以下方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == kContactsSection) {
        NSArray     *contacts       = self.contacts;
        NSUInteger  contactCount    = [contacts count];

        return contactCount;
    }

    return [super tableView:tableView numberOfRowsInSection:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger   section = indexPath.section;
    NSInteger   row     = indexPath.row;

    if (section == kContactsSection) {
        return [super tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:kContactsSection]];
    }

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger   section     = indexPath.section;

    if (section == kContactsSection) {
        CGFloat indentation = [super tableView:tableView indentationLevelForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:kContactsSection]];

        return indentation;
    }

    CGFloat     indentation = [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath];

    return indentation;
}