• xcode4.3.1定制UITableviewCell实现类似微博的分组效果


    最早学习定制UITableViewCell的时候,被各种半吊子的教程折磨的不轻,也没一个能运行的代码,只能自己捉摸,现在有空了,准备总结一下,主要是为了演示整个自定义UITableViewCell的过程,后面会给源文件。当然了,也要有个努力的目标:微博的个人资料的关注,微博,粉丝,话题那个小分组框。
    第一步创建一个工程,我用是的”Master-Detail Application”,然后去了根本不用东东,显的更简洁,原始代码下载
    准备完了,开始:
    1.“New File”,选中“Objective-C class”,下一步

    2.填写上文件名,这里是SinaTableCell,然后下一步

    3.直接“Create”就好了,没啥好说的

    这样的话,我们就得了实现文件

    可是对于我来说,我还是倾向于使用ib画界面。接下来咱们再创建一个xib文件。
    1.同样“New File”

    2.“User Interface”->“Empty”->“Next”

    3.Device Family保持默认,“Next”

    7.只在的文件名,和类文件一致,用“SinaTableCell”就好了。“Creat”

    这样我就把.h,.m,.xib三个文件凑起了。

    把.h,.m和.xib文件连接起来.
    选中xib文件,在里添加一个Table View Cell控件

    选中新添加的Table View Cell,在属性里指定Class为:SinaTableCell

    指定TableViewCell的Identifier

    创建4个141×41的button,(不要问我咋创建),并指定背景图片

    这样xib操作就完事儿了。

    MasterViewController.m里的主要实现

    #import "SinaTableCell.h"
    #pragma mark - Table View
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
      return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      return 1;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
      return 85;
    }
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView
             cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      static NSString *_customIdentifier = @"SinaTableCell";
      SinaTableCell *cell=(SinaTableCell *)[tableView dequeueReusableCellWithIdentifier:_customIdentifier];
       
        if (cell==nil) {
            NSArray *_nib=[[NSBundle mainBundle] loadNibNamed:@"SinaTableCell"
                                                    owner:self
                                                  options:nil];
        cell = [_nib objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
      return cell;
    }

    看一下运行结果,

    与微博里的分组看着差不多就可以了,说明自定义成功!

    完整工程下载

  • 相关阅读:
    sqlite3中给表添加列
    webpack打包后服务端__dirname失效问题
    nodejs查看本机hosts文件域名对应ip
    vscode远程调试node服务端
    git 删除错误commit
    npm安装模块没有权限解决办法
    node gyp编译所需要的环境
    npm和yarn的淘宝镜像添加
    笨办法学Python 笔记
    梯度下降法优化线性回归算法参数
  • 原文地址:https://www.cnblogs.com/cpcpc/p/2574748.html
Copyright © 2020-2023  润新知