首先需要初始化table,
UITableView * table;? 這個我感覺申請成全局變量好一些
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
由于要設置 table.delegate = self;??????? table.dataSource = self;
所以在最上面interface旁邊要加上?????? 數據源和委派<UITableViewDelegate,UITableViewDataSource>
記得將初始化好的table加入到? self.view中
然后開始進行別的函數
//在interface旁邊加了 數據源和委派? 之后才能用這個
//這個函數是返回組數
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
? ? return 1;
}
//這個函數是返回一組內cell的個數
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
??? return 4;
}
//然后才進行生成或者是在數據池里尋找cell
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
?????????? static NSString * cellId = @"cellId";//首先定義一個字符串對象,用來做cell的標記,下面會用到
????????? //? 然后在數據池里找標記為cellId的 cell,找到,就直接顯示出來,沒找到,就要初始化這個cell 了
????????? UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
???????? if(!cell)? //如果沒找到
?????? {
??????????????????? cell=[[UITableViewCell? alloc]init];//初始化生成cell
???????? }
?????? tableView.rowHeight = 200;//定義cell的高度
?????? NSInteger row1 = indexPath.row;//獲取cell的索引
?????? 然后再加cell上的其他東西
???? 最后返回cell
??? return??? cell;
}