1.创建基于View-based Application的工程,添加一个UITableView控键.
2. .h文件中加入委托协议 UITableViewDelegate,UITableViewDataSource 并链接
3. .m文件中方法实现:代码如下:
重写VIewDidLoad方法
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"The First One",@"The Second One",@"The Third One",@"The Four one", nil];
self.listData = array;
[array release];
}
@end 之前加入如下代码:
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section //返回目标行数
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath //返回每行的内容
{
static NSString *thekey = @"thekey"; //定义键值标示符
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:thekey];
if(cell == Nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:thekey] autorelease];
}
NSUInteger row = [indexPath row];
UIImage image = [UIImage imageNamed:@"apple.png"];
cell.image = image; //添加cell前的图像
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
#pragma mark Table Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath //缩进方法
{
NSUInteger row = [indexPath row];
return row;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath //行选择
{
NSUInteger row = [indexPath row];
if(row == 0){
return nil;
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//选择后执行的事件
{
NSUInteger row = [indexPath row];
NSString *Value = [listData objectAtIndex:row];
NSString *showMessage = [[NSString alloc] initWithFormat:@"%@" ,Value];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Select" message:showMessage delegate:self cancelButtonTitle:@"Yes" otherButtonTitles: nil];
[alert show];
[alert release];
[showMessage release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath //改变行高
{
return 150;
}
494

被折叠的 条评论
为什么被折叠?



