也许您对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.
}
注释很简单,虽然是英文但我们看一下函数名就知道其作用了,这里就不过多解释了。
效果图:
好了就写这么多,有什么问题请留言,大家一起学习交流!
说明:转载请注明出处!