UITableViewController-简单自定义单元格

本文介绍了一个自定义UITableViewCell的实现过程,包括如何创建自定义单元格、设置按钮点击回调以及在控制器中使用这些自定义单元格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  MyUItableViewCell.h
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/23.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import <UIKit/UIKit.h>
typedef void (^ButCallBack) (id obj);

@interface MyUItableViewCell : UITableViewCell

@property(nonatomic,strong) ButCallBack butCallBack;
@property(nonatomic,strong) UITextField *textField;
@property(nonatomic,strong) UIButton *btn;

@end
//
//  MyUItableViewCell.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/23.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "MyUItableViewCell.h"

@implementation MyUItableViewCell

@synthesize butCallBack;
@synthesize textField;
@synthesize btn;

- (instancetype)initWithStyle:(UITableViewCellStyle)styleA reuseIdentifier:(NSString *)reuseIdentifierA{

    self = [super initWithStyle:styleA reuseIdentifier:reuseIdentifierA];
    if (self != nil) {
        [self createView];
    }
    return self;
}

- (void)createView{
    textField= [[UITextField alloc] init];
    textField.frame = CGRectMake(5, 10, 80, 20);
    [self addSubview:textField];
    
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 10, 100, 30);
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}

- (void)btnClick:(UIButton *)btnEntity{
  
    self.butCallBack(btnEntity); // 调用接口, 实现类自己去实现。

}



@end

//
//  MyTableViewController.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/23.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "MyTableViewController.h"
#import "MyUItableViewCell.h"

@implementation MyTableViewController

@synthesize array;


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"主菜单";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

// 常用的table的属性
- (void)createTableView{
    array = [NSMutableArray arrayWithObjects:@"韩红",@"张国荣",@"刘德华",@"许巍", nil];
}


// 确定表视图有多少个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

// 分区头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"分区1号";
    }
    return @"分区2号";
}

// 分区尾
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return @"分区尾部1号";
    }
    return @"分区尾部2号";

}



// 区里面有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.array.count;
}

// 设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    // 查看内存中是否有
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
//    if (cell == nil) {
//        // 样式决定你显示的形式
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
//    }
//    
//    cell.textLabel.text = self.array[indexPath.row];
//    // 设置颜色
//    cell.textLabel.textColor = [UIColor blueColor];
//    // 对于显示的字体的大小和颜色,都是在这里进行设置的。
//    cell.detailTextLabel.text = @"男歌星";
//    cell.accessoryType = UITableViewCellAccessoryCheckmark;
//    cell.imageView.image = [UIImage imageNamed:@"tupian5.jpg"];
    
    // 自定义单元格
    MyUItableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    ButCallBack callback =  ^(id obj){
        if ([obj isKindOfClass:[UIButton class]]) {
            UIButton *btnObj = (UIButton *)obj;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"点击了" message:@"我点" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"OK", nil];
            [alert show];
            NSLog(@"回调成功----%@-----",btnObj.titleLabel.text);
        }
    };
    
    if (cell == nil) {
        cell = [[MyUItableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
    }
    [cell setButCallBack:callback];
    cell.textField.text = self.array[indexPath.row];
    NSString *btnMessage = self.array[indexPath.row];
    btnMessage = [btnMessage stringByAppendingFormat:@"-按钮-%ld",indexPath.row];
    [cell.btn setTitle:btnMessage forState:UIControlStateNormal];
    return cell;
}

// 设置缩进-- 显示的文字缩进
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return  indexPath.row;
}

// 单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}

// 取消选中某一行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

   
    NSLog(@"取消%ld",indexPath.row);

}

// 选中某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   NSLog(@"选中%ld",indexPath.row);

}


// 删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

//    NSLog(@"删除触发");
    
    // 数据删除
    [array removeObjectAtIndex:indexPath.row];
    // 界面删除
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    
    

}







@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值