微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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





版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐