IOS学习之——Cell自定义内容视图&自定义辅助视图

自定义内容视图

step1:创建显示内容的视图

step2: 将这些视图以子视图的形式,添加到cell.contentView 中即可


3.自定义辅助视图

step1:创建视图

step2:将视图 赋值给 cell.accessoryView 即可



//
//  MyTableViewController.m
//

#import "MyTableViewController.h"

@interface MyTableViewController ()

@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //为tableView 注册单元格类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

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

#pragma mark - Table view data source

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

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    /****** 自定义内容视图  **************/
    UILabel *label = [cell.contentView viewWithTag:120];
    if (label == nil){ //如果 cell中没有我们自己定义的 这个Label, 那么我就创建一个添加到cell中
        label = [[UILabel alloc]init];
        label.tag = 120;
        label.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);
        label.font = [UIFont italicSystemFontOfSize:36];
        label.textColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label];
    }
    
    label.text = [NSString stringWithFormat:@"该行是 %ld行",indexPath.row];
    
    
    /***** 自定义辅助视图 ****************/
    if (indexPath.row == 2) {
        UISwitch *myswitch = [[UISwitch alloc]init];
        myswitch.on = YES;
        [myswitch addTarget:self action:@selector(clickSwitch:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = myswitch;
    }else {
        cell.accessoryView = nil;
    }
   
    return cell;
}
-(void)clickSwitch:(UISwitch*)sender {
    NSLog(@"当前是 %@ ", sender.on ? @"开" : @"关");
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值