美文网首页iOSiOS高质量博客
Storyboard中的静态(static)cell和动态(dy

Storyboard中的静态(static)cell和动态(dy

作者: 巧克力的qiao | 来源:发表于2017-04-14 15:10 被阅读963次

前言

在开发中,很多时候会遇到一些复杂的列表界面,有一些数据是固定展示的,而有些是不固定的,这时候如果能够使用static TableView 与动态cell结合使用,会很快的提高开发效率,比如说系统的搜索WiFi界面:

搜索WiFi前 搜索WiFi后

其中无线局域网开关及下面的非动态cell 可以用静态单元格画出来,而搜索到的WiFi则需要通过动态加载的方式来实现。

进入正题

�例.gif

一、OC语言实现过程

storyboard的创建过程,在此我就直接拖图展示了,过多解释在此不细说了

Snip20170414_6.png

在此以第二组为动态改变的cell
TableViewController中注意重写以下方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (section==1) {
    return self.dataSource.count;
}
return [super tableView:tableView numberOfRowsInSection:section];
}


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

if (indexPath.section==1) {
    
    XYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XYTableViewCell" forIndexPath:indexPath];
    cell.lab_content.text = self.dataSource[indexPath.row];
    
    return cell;

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

 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section==1) {
    return 50;
}
return 0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

return self.tableView.rowHeight;
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  
if (section==1){
    CGFloat w = self.view.bounds.size.width/2.0;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2*w, 50)];
    
    headerView.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, w, 50)];
    [btn1 setTitle:@"评论" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor redColor];
    [headerView addSubview:btn1];
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(w, 0,w, 50)];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn2.backgroundColor = [UIColor orangeColor];
    [btn2 setTitle:@"咨询" forState:UIControlStateNormal];
    [headerView addSubview:btn2];
    
    
    return headerView;
}
return nil;
}

当覆盖storyboard中的staticcell时,因为数据源对新加进来cell的层级一无所知,所以需要写这个代理方法。否则会导致崩溃
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 1) {
return [super tableView:tableView indentationLevelForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
}
return [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
}

若报出这种错误Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]',查看你的代码是否漏写了tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath方法,然后完善即可。
具体原因链接说明查看此处

一、swift语言实现过程

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section==1 {
        return self.dataSource.count
    }
    return super.tableView(tableView, numberOfRowsInSection: section)
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.section==1 {
        let cell:XYTableViewCell = tableView.dequeueReusableCell(withIdentifier: "XYTableViewCell", for: indexPath) as! XYTableViewCell
        cell.lab_content.text = self.dataSource[indexPath.row] as? String
        return cell
    }

    return super.tableView(tableView, cellForRowAt: indexPath)
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    return self.tableView.rowHeight
    
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section==1 {
        return 50
    }
    return 0
}


override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 1 {
        let w = self.view.bounds.size.width/2.0
        let headView = UIView(frame: CGRect(x: 0, y: 0, width: 2*w, height: 50))
        headView.backgroundColor = UIColor.white
        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: w, height: 50))
        btn1.setTitle("评论", for: .normal)
        btn1.backgroundColor = UIColor.red
        headView.addSubview(btn1)
        
        let btn2 = UIButton(frame: CGRect(x: w, y: 0, width: w, height: 50))
        btn2.setTitle("咨询", for: .normal)
        btn2.setTitleColor(UIColor.white, for: .normal)
        btn2.backgroundColor = UIColor.red
        headView.addSubview(btn2)
        
        return headView

    }
    return nil
}
//道理同上
override func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
    if indexPath.section == 1 {
        let newIndexPath = IndexPath(row: 0, section: indexPath.section)
        return super.tableView(tableView, indentationLevelForRowAt: newIndexPath)
    }
    return super.tableView(tableView, indentationLevelForRowAt: indexPath)
}

相关文章

网友评论

  • saman0:你的demo有错,添加这一行代码。self.tableView.rowHeight = UITableViewAutomaticDimension
  • Hello_kid:因为数据源对新加进来的cell一无所知,所以需要写这个代理方法。否则会导致崩溃 这句话没看懂哈、 还有如果不重写行高的代理方法好像也会奔溃啊、?能说的通俗易懂谢吗
    巧克力的qiao:@Ruiz678 已在文章中修改了,是对新加cell的层级一无所知。不重写行高导致崩溃,等我晚上看看,这会儿忙。。。

本文标题:Storyboard中的静态(static)cell和动态(dy

本文链接:https://www.haomeiwen.com/subject/mbfwattx.html