混合静态和动态表格视图单元格iOS [英] Mix of static and dynamic table view cells iOS

查看:152
本文介绍了混合静态和动态表格视图单元格iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个包含静态和动态原型单元格的表视图。



所以,我想要实现的是这样的:

 静态
静态
静态
静态
动态
...

此帖子 Stackoverflow UITableView静态和动态单元的混合?,有点帮助,但它无法覆盖我的整个问题。



我想要4个静态单元格,但我想在这些单元格中包含控件并将它们作为出口连接,然后在下面添加更多动态单元格。 / p>

这可能吗?



提前谢谢。



编辑:我到目前为止的方法


  1. 创建一个表视图只有静态单元格,然后在原始单元格中添加新的表格视图。但是,这种方法看起来并不专业,而且,当我将数据源和委托添加到新的表视图时,原始表视图向下移动。


  2. 为静态和动态单元格创建两个表视图,然后将它们包含在一个视图中。这是我认为合理的方式,但因为我是新手开发人员,无法找出如何解决静态表视图应该嵌入在表视图控制器中。



解决方案

您可以继承 UITableViewController 并覆盖dataSource和委托很好地实现这一目标的方法。



关键是将呼叫转发到 super ,其中需要静态内容,在需要动态内容时做自己的事情。



例如,使用IB来定义带有静态的 UITableViewController 以正常方式进行内容,然后按如下子类添加一个带有动态内容的额外部分:

   - (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView 
{
return [super numberOfSectionsInTableView:tableView] + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section< [super numberOfSectionsInTableView:tableView] ){
return [super tableView:tableView numberOfRowsInSection:section];
}
else {
返回self.numberOfRowsInMyDynamicSection;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath。 section< [super numberOfSectionsInTableView:tableView]){
return [super tableVIew:tableView cellForRowAtIndexPath:indexPath];
}
else {
//做你自己的动态细胞管理;
}
}

//等。其他dataSource和委托方法

我用这种方法创建 UITableViewController 子类,允许您在运行时显示/隐藏静态定义的单元格。



实现所有dateSource /需要花费一些精力委托方法,但最终得到一个非常方便的 UITableViewController 子类。


I was trying to create a table view with static and dynamic prototype cells.

So, the thing I want to achieve is like this:

static
static
static
static
dynamic
...

This post on Stackoverflow, UITableView Mix of Static and Dynamic Cells?, was kind of helpful but it couldn't cover my whole problem.

I want 4 static cells, but also I would like to include controls inside those cells and connect them as outlets, and then add more dynamic cells below.

So is this possible?

Thank you in advance.

EDIT: Approaches I've taken so far

  1. Create a table view with only static cells and then add a new table view inside the original one. However, this approach doesn't look professional, and moreover, when I added datasource and delegate to that new table view, the original table view moved down.

  2. Create two table views for static and dynamic cells, and then include them in one view. This is I think the plausible way, but because I'm a novice developer, couldn't find out how to solve the "static table views should be embedded within a table view controller".

解决方案

You can subclass UITableViewController and override the dataSource and delegate methods to achieve this nicely.

The key is to forward calls on to super where the static content is required and do your own thing when dynamic content is required.

For example, use IB to define a UITableViewController with your static content in the normal way but then subclass as follows to add a single extra section with dynamic content:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [super numberOfSectionsInTableView:tableView] + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section < [super numberOfSectionsInTableView:tableView]) {
        return [super tableView: tableView numberOfRowsInSection:section];
    }
    else {
        return self.numberOfRowsInMyDynamicSection;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section < [super numberOfSectionsInTableView:tableView]) {
        return [super tableVIew: tableView cellForRowAtIndexPath: indexPath];
    }
    else {
        // do your own dynamic cell management;
    }
}

//etc. for the other dataSource and delegate methods

I've used this technique to create a UITableViewController subclass that allows you to show/hide statically defined cells at runtime.

It takes a bit of effort to implement all the dateSource/delegate methods, but you end up with a really handy UITableViewController subclass.

这篇关于混合静态和动态表格视图单元格iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆