iphone開發之TableView控制項執行個體

來源:互聯網
上載者:User

也許您對android中ListView控制項很熟悉,ListView為我們展示了一個列表形式的資料,在ios下的TableView為我們實現同樣的功能。TableView實現起來很簡單,下面看具體例子。建立一個項目,我們要在ViewController.h添加如下代碼:

<UITableViewDelegate, UITableViewDataSource>

顯而易見,這樣做的目的是為了為TableView添加資料,和實現UITableView委託方法。在ViewController.m中添加如下代碼:

#pragma mark - View lifecycle- (void)viewDidLoad       //{    [super viewDidLoad];    self.title = @"First Level";    NSArray *array = [[NSArray alloc] initWithObjects:@"Kobe", @"James", @"Wade", @"Rose", @"Yao", nil];  //Set tableViewCell's text     self.listData = array;    [array release];}- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;    self.listData = nil;}- (void) dealloc{    [self.listData release];    [super dealloc];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{   // Return the number of rows in the section.    return [self.listData count];     //}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"Cell";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];    }        // Configure the cell...    NSUInteger row = [indexPath row];    cell.textLabel.text = [self.listData objectAtIndex:row];    cell.detailTextLabel.text = [self.listData objectAtIndex:row];
    UIImage *image = [UIImage imageNamed:@"wolfSpiderThumb.jpg"];    cell.imageView.image = image;    return cell;}/*// Override to support conditional editing of the table view.- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    // Return NO if you do not want the specified item to be editable.    return YES;}*//*// Override to support editing the table view.- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        // Delete the row from the data source        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];    }       else if (editingStyle == UITableViewCellEditingStyleInsert) {        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view    }   }*//*// Override to support rearranging the table view.- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{}*//*// Override to support conditional rearranging of the table view.- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    // Return NO if you do not want the item to be re-orderable.    return YES;}*/#pragma mark - Table view delegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // Navigation logic may go here. Create and push another view controller.         }

注釋很簡單,雖然是英文但我們看一下函數名就知道其作用了,這裡就不過多解釋了。

好了就寫這麼多,有什麼問題請留言,大家一起學習交流!

說明:轉載請註明出處!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.