IOS自定义表格UITableViewCell

简介:

在UITableView中,自定义表格,最原始是继承UITableViewCell,然后通过写代码方式去搞,但是这个费事了。

1.在storyboard中

给一个ViewController的tabieview增加自定义的UITableViewCell,可以直接从 object Library里面选取UITableViewCell拖动到tableview中,然后添加界面上自定义元素,然后补充cell的类,重用id等信息。

补充完成后,需要在工程中添加对应的cell的类文件,并做代码和xib的关联。

@interface DemoCellOne : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageInfoView;
@property (weak, nonatomic) IBOutlet UILabel *contentInfoLabel;


@end

然后就可以在相应的viewcontroller里面使用了,如下:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DemoCellOne *cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellOne"];
    cell.contentInfoLabel.text = [self.datasource objectAtIndex:indexPath.row];
    return cell;
}

 

2.在普通的xib文件中

如果ios工程还是之前那种xib形式的,则可以给工程添加新文件,添加时候选择添加新的类,从UITableViewCell继承,然后在生成源码文件之前,先在确认界面勾选上生成对应的xib文件。

生成好之后,在xib中给UITableViewCell添加个性化元素,然后在代码中加载。

 

以下是cell对应的类的定义,为了便于修改,做了xib和代码之间的IBOutlet关联。

1 @interface DemoCellTwoTableViewCell : UITableViewCell
2 @property (weak, nonatomic) IBOutlet UILabel *infoLabel;
3 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
4 
5 @end

 

以下是在viewcontroller中使用

复制代码
@interface ViewController2 ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *contentTableView;
@property(nonatomic,strong) NSMutableArray * datasource;
@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.contentTableView registerNib:[UINib nibWithNibName:@"DemoCellTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"DemoCellTwoTableViewCell"];
    self.datasource = [NSMutableArray array];
    [self loadDataSource];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadDataSource
{
    [self.datasource addObject:@"a1"];
    [self.datasource addObject:@"a2"];
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DemoCellTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellTwoTableViewCell"];
    cell.infoLabel.text = [self.datasource objectAtIndex:indexPath.row];
    return cell;
}

@end
复制代码

上面是通过注册tableview的cell对应的nib文件的方式来重用cell的。还有一种方式如下:

复制代码
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DemoCellTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellTwoTableViewCell"];
    if (nil == cell)
    {
        NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"DemoCellTwoTableViewCell" owner:self options:nil];
        cell = [objs objectAtIndex:0];
    }
    cell.infoLabel.text = [self.datasource objectAtIndex:indexPath.row];
    return cell;
}
复制代码

这种方式不需要在viewcontroller的viewdidload方法里面注册重用的nib文件。只是在cell重用处加载nib。

 

注意:第二种方式,在自定义cell的xib文件中,file owner不需要修改,保持默认值就行了。



本文转自leipei博客园博客,原文链接:http://www.cnblogs.com/leipei2352/p/4358377.html,如需转载请自行联系原作者

目录
相关文章
|
3月前
|
iOS开发 UED
实现一个自定义的iOS动画效果
【4月更文挑战第9天】本文将详细介绍如何在iOS平台上实现一个自定义的动画效果。我们将通过使用Core Animation框架来实现这个动画效果,并展示如何在不同的场景中使用它。文章的目标是帮助读者理解如何使用Core Animation框架来创建自定义动画,并提供一个简单的示例代码。
32 1
|
7天前
|
Swift iOS开发 UED
【绝妙创意】颠覆你的视觉体验!揭秘一款iOS应用中令人惊叹的自定义动画效果,带你领略编程艺术的魅力所在!
【8月更文挑战第13天】本文通过一个具体案例,介绍如何使用Swift与UIKit在iOS应用中创建独特的按钮动画效果。当按钮被按下时,其形状从圆形变化为椭圆形,颜色则从蓝色渐变为绿色;释放后,动画反向恢复原状。利用UIView动画方法及弹簧动画效果,实现了平滑自然的过渡。通过调整参数,开发者可以进一步优化动画体验,增强应用的互动性和视觉吸引力。
24 7
|
3月前
|
iOS开发
iOS UITableViewCell刷新某些行的cell或section
iOS UITableViewCell刷新某些行的cell或section
29 0
|
11月前
|
iOS开发
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
333 0
|
11月前
|
API iOS开发
iOS 自定义转场动画 UIViewControllerTransitioning
iOS 自定义转场动画 UIViewControllerTransitioning
74 0
|
Swift iOS开发
iOS 13 之后自定义 Window 不显示解决 (SceneDelegate)
iOS 13 之后自定义 Window 不显示解决 (SceneDelegate)
308 0
|
Linux iOS开发 开发者
WIN11自定义版本ios镜像下载教程
WIN11自定义版本ios镜像下载教程
WIN11自定义版本ios镜像下载教程
|
API iOS开发 Perl
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
|
API iOS开发 Perl
iOS UILabel自定义位置
iOS UILabel自定义位置
iOS UILabel自定义位置
|
iOS开发
iOS开发-banner滚动图自定义
iOS开发-banner滚动图自定义
196 0
iOS开发-banner滚动图自定义