UIKit-TableView--TableView用代码自定义cell

上一篇博客传送门:UIKit-TableView-简单的TableView细节扩展
这一篇我们来实现用代码自定义我们的cell单元格

  • 先自定义一个cell子类
  • 自定义我们需要的子控件
  • 去到viewController当中使用我们的子类
  • 并且使用我们的子控件

    以下为自定义类代码:

import UIKit
//定义一个自己的cell类
class CityTableViewCell: UITableViewCell {


    //定义我们需要的UI控件
    var cityLabel: UILabel?
    var cityTextField: UITextField?
    var citySwitch: UISwitch?
    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //初始化子视图:子控件 然后添加到当前视图
        cityLabel = UILabel(frame: CGRect(x: 5, y: 0, width: 40, height: 40))
        cityTextField = UITextField(frame: CGRect(x: 50, y: 0, width: 200, height: 40))
        citySwitch = UISwitch(frame: CGRect(x: 255, y: 0 , width: 40, height: 40))
        //然后将子控件添加到子视图
        self.addSubview(cityLabel!)
        self.addSubview(cityTextField!)
        self.addSubview(citySwitch!)
        //然后我们就可以使用我们自定义的这个子类了
    }
    //此为我们必须实现的父类构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

然后在viewController当中使用自定义子类:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{

    @IBOutlet weak var myTableView1: UITableView!
    //定义一个城市数组
    var cities = ["北京","上海","广州","深圳"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        myTableView1.dataSource = self
        myTableView1.delegate = self
    }

    //设置表格中有2个section
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return cities.count
    }


    @available(iOS 2.0, *)
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        //先定义一个能够重复使用的cell标识符
        let cellid = "jycell"
        //dequeue为出列的意思,这里为让我们的可重复使用的cell出列
        var cell: CityTableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellid) as? CityTableViewCell
        //判断cell是否为nil
        if cell == nil {
            //如果为nil则创建一个subtitle样式的cell
            cell = CityTableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: cellid)
        }
        //以下为设置我们UI控件的值
        cell?.cityLabel?.text = cities[indexPath.row]
        cell?.cityTextField?.text = "input number.."
        cell?.citySwitch?.isOn = true
        return cell!
    }


}

最后运行模拟器即可得到以下:
这里写图片描述

下一篇博客传送门:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过使用 UITableView 来实现一个日历表格。具体实现步骤如下: 1. 创建一个继承自 UIViewController 的类,例如名为 `CalendarViewController`。 2. 在 `CalendarViewController` 中创建一个 UITableView,并将其添加到当前视图中。 3. 实现 UITableViewDataSource 和 UITableViewDelegate 协议中的方法,以便为 UITableView 提供数据和响应用户的操作。 4. 在 UITableView 中创建 UITableViewCell,并为其添加子视图,例如 UILabel,来显示日期等信息。 5. 根据需要,可以使用 NSDate 和 NSCalendar 等类来计算日期和星期等信息。 6. 在 UIViewController 的 viewDidLoad 方法中,设置 UITableView 的数据源和代理为当前的 `CalendarViewController` 对象。 下面是一个简单的示例代码,可以根据需要进行修改和扩展: ``` // CalendarViewController.h #import <UIKit/UIKit.h> @interface CalendarViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @end // CalendarViewController.m #import "CalendarViewController.h" @interface CalendarViewController () @end @implementation CalendarViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建 UITableView self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.delegate = self; [self.view addSubview:self.tableView]; } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 返回日历表格的总共行数 return 6; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回每一行日历表格的列数 return 7; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CalendarCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // 计算当前单元格对应的日期 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.day = indexPath.row + indexPath.section * 7; NSDate *date = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0]; // 显示日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"d"; cell.textLabel.text = [formatter stringFromDate:date]; return cell; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } @end ``` 在使用时,可以创建一个 `CalendarViewController` 对象,并将其添加到当前视图中即可: ``` CalendarViewController *calendarViewController = [[CalendarViewController alloc] init]; [self addChildViewController:calendarViewController]; [self.view addSubview:calendarViewController.tableView]; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值