现在的位置: 首页 > 综合 > 正文

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

2018年07月26日 ⁄ 综合 ⁄ 共 1673字 ⁄ 字号 评论关闭

最早学习定制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;
}

看一下运行结果,

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

完整工程下载

抱歉!评论已关闭.