ios uitableview的簡(jiǎn)單使用(一)

1.UITableView的屬性控件和數(shù)組

#pragma mark - 表格
@property (nonatomic,strong)UITableView *tableview;
#pragma mark - 數(shù)據(jù) (NSMutableArray 可變數(shù)組;NSArray 數(shù)組)
@property (nonatomic,strong)NSMutableArray *dataArry;

2.在@implementation中實(shí)現(xiàn),或者叫初始化表格個(gè)數(shù)組對(duì)象,代碼:

-(UITableView *)tableview{
    if(_tableview == nil){
        //bounds 和 frame 區(qū)別:bounds,指的是空間本身大小,x=0,y=0;frame,x指的是在父控件的位置和大小
        _tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//        [UIScreen mainScreen].bounds;指的是屏幕大小
        _tableview.dataSource = self;//遵循數(shù)據(jù)源
        _tableview.delegate = self;//遵循協(xié)議
    }
    return _tableview;
}
-(NSMutableArray *)dataArry{
    if(_dataArry == nil){
        _dataArry = [NSMutableArray array];//初始化數(shù)據(jù)
    }
    return _dataArry;
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
//表格組數(shù) Sections 組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

3.遵循數(shù)據(jù)源(UITableViewDelegate)和代理(UITableViewDataSource)方法

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
#pragma mark - 表格
@property (nonatomic,strong)UITableView *tableview;
#pragma mark - 數(shù)據(jù) (NSMutableArray 可變數(shù)組;NSArray 數(shù)組)
@property (nonatomic,strong)NSMutableArray *dataArry;

4.實(shí)現(xiàn)數(shù)據(jù)源(UITableViewDelegate)和代理(UITableViewDataSource)方法,代碼:

-(NSMutableArray *)dataArry{
    if(_dataArry == nil){
        _dataArry = [NSMutableArray array];//初始化數(shù)據(jù)
    }
    return _dataArry;
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
//表格組數(shù) Sections 組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//每組返回行數(shù) Rows 行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArry.count;
}
//每個(gè)單元格的e內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
        
        
    }
    model *m =self.dataArry[indexPath.row];//取出數(shù)據(jù)元素
    cell.textLabel.text = [NSString stringWithFormat:@"姓名:%@,年齡:%@",m.name,m.age];
    return cell;
}

注意:使用自定義cell時(shí),注冊(cè)方法在viewdidLoad中添加,并且需要在viewDidLoad中將表格添加到主視圖。代碼

- (void)viewDidLoad {
    [super viewDidLoad];
//    _tableview = [[UITableView alloc]init];
//    _tableview.frame = self.view.bounds;
    
    //添加數(shù)據(jù)元素
    model * m0 =[[model alloc]init];
    m0.name = @"張三";
    m0.age = @"16";
    [self.dataArry addObject:m0];
    //    [self.dataArry addObject:m0];
    //[self.dataArry addObject:@"第一組"];
    //[self.dataArry addObject:@"第二組"];
    [self.view addSubview:self.tableview];//添加表格到視圖
    

補(bǔ)充,模型的代碼如下

@interface model : NSObject
@property(nonatomic,copy)NSString *name;//姓名
@property(nonatomic,copy)NSString *age;//姓名
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容