iphone開發之TableView控件實例

也許您對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.
    
     
}
註釋很簡單,雖然是英文但我們看一下函數名就知道其作用了,這裏就不過多解釋了。
效果圖:



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

說明:轉載請註明出處!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章