ios 表視圖

來源:互聯網
上載者:User

分組表中每個分組都是一個分區,在索引表中,每個索引都是一個分區。

實現一個簡單的表:

首先在xib檔案中拖入一個tableview,將其dataSource和delegate串連到File‘s owner


@interface Hello_WorldViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{

}

上述代碼的作用是讓類遵循兩個協議,類需要這兩個協議來充當表現圖的委託和資料來源,資料來源提供了繪製表所需要的所有資料,委託用於配置表視圖的外觀
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listData.count;
}
上述方法用來指明該section有幾個表項,預設section值為1

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier=@"simpleIdentifier";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:simpleTableIdentifier];
    }
    NSUInteger row=[indexPath row];
    cell.text=[self.listData objectAtIndex:row];
    return  cell;
}

上述函數用來繪cell,第一個參數是代表用來繪cell的發起者,第二個參數是indexPath,通過該參數我們可以得到section和row,

表視圖在iphone上一次只能顯示有限的幾行,但是表示圖的資料量可以是很大的,如果我們為每一行資料量分配一個cell,那麼開銷將非常大。

同時我們發現,有些cell是當前顯示,有些不是,當一個cell滾出螢幕時,另一些cell就會從另一邊滾到螢幕上,如果滾到螢幕上的cell重新使用滾出螢幕的cell,那麼系統就不會為建立或刪除一個新的cell而浪費空間與時間。我們用simpleTalbeIdentifier來標示每個cell,調用[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier] ,看有這個標示的cell是否以前在可重用隊列中出現過,如果有那直接使用即可,如果沒有則為nil,cell=[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:建立新的cell。

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath row];
    NSLog(@"setion is %d",section);
    NSString *rowValue=[self.listData objectAtIndex:section];
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"你選擇的是" message:rowValue delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
    [alertView show];
}
 上述方法表示點擊一個cell時調用

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

上述方法根據indexPath給出每個cell的高度

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.