IOS 自定义UITableView

根据不同需要,需要使用tableview的结构,但是里面每一个cell,又需要自己的样式,所以学习了一下怎样把自己定义的cell加到tableview里面



首先要自己创建一个类,继承UITableViewCell,然后新建一个空的xib文件,并在class属性设置为对应的类名







代码部分:

<pre name="code" class="objc">

#import "SettingViewController.h"
#import "SettingViewCell.h"
@interface SettingViewController ()

@end

@implementation SettingViewController
@synthesize listArray =_listArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSArray *array = [NSArray arrayWithObjects: @"房间设置",@"灯光设置",@"窗帘设置",@"场景设置",@"空调设置",@"安防设置",@"网络设置",nil];
    _settingview.dataSource =self; //设置数据源方法
    _settingview.delegate =self;//设置代理方法
    self.listArray =array; //注意,由于内存管理问题,如果不对self.listArray进行赋值,则_listArray会被清空,程序会崩溃
 
}
- (IBAction)backClicked:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
//数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [_listArray count];
}

//委托方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch(indexPath.row)
    {
        case 0:
            break;
        default:
            break;
    }
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier =@"mycell";
    SettingViewCell  *cell =(SettingViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        NSArray *array =[[NSBundle mainBundle] loadNibNamed:@"SettingViewCell" owner:self options:nil];
        cell = [array objectAtIndex: 0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    }
    
    NSString *text = _listArray[indexPath.row];
 
    [[cell settingName] setText:text];
    return cell;
}


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

- (void)dealloc {
    [_back release];
    [_settingview release];
    [super dealloc];
}
@end


 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值