你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 自定義UITableView

IOS 自定義UITableView

編輯:IOS開發綜合

根據不同需要,需要使用tableview的結構,但是裡面每一個cell,又需要自己的樣式,所以學習了一下怎樣把自己定義的cell加到tableview裡面

\

\

首先要自己創建一個類,繼承UITableViewCell,然後新建一個空的xib文件,並在class屬性設置為對應的類名<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"" alt="\">

\


\

\


代碼部分:


#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





  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved