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.
    
     
}
注释很简单,虽然是英文但我们看一下函数名就知道其作用了,这里就不过多解释了。
效果图:



好了就写这么多,有什么问题请留言,大家一起学习交流!

说明:转载请注明出处!



JavaFX 的 TableView 控件是一个用于展示表格数据的高级控件。它提供了灵活的列和行布局,支持对表格数据进行排序、筛选、编辑和选择等操作。 TableView 的主要特点和功能包括: 1. 列(Columns):TableView 可以包含多个列,每个列由 TableColumn 对象表示。你可以添加、移除和重新排序列,设置列的宽度、标题、对齐方式等属性。 2. 行(Rows):TableView 的每一行表示表格中的一条数据记录。可以通过添加到 TableView 的数据集合(ObservableList)来动态添加和删除行。 3. 单元格(Cells):每个单元格用于显示一个数据项。你可以自定义单元格的样式,包括字体、颜色、背景等。同时,TableView 也支持对单元格进行编辑。 4. 数据源(Items):TableView 使用一个 ObservableList 作为数据源,它可以是任何实现了 javafx.collections.ObservableList 接口的类。 5. 排序和筛选:TableView 支持对表格数据进行排序和筛选。你可以通过设置 TableColumn 的 sortable 属性来启用或禁用某列的排序功能。 6. 编辑(Editing):TableView 可以设置为可编辑模式,允许用户直接在表格中修改数据。你可以通过设置 TableColumn 的 editable 属性来控制某列是否可编辑。 7. 选择(Selection):可以通过 TableView 的 SelectionModel 获取用户选择的行或单元格信息。支持单选和多选模式。 8. 自定义单元格显示:你可以通过 setCellFactory 方法来自定义单元格的显示方式,实现更复杂的表格布局和样式。 使用 TableView 控件,你可以将表格数据以更直观、灵活和可交互的方式展示给用户,满足各种数据展示和操作的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值