iOS探索--TableView的使用

一、简介
UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。大家先看一下两者的应用:
这里写图片描述
二、代码示例
1.TableViewController.xib
这里写图片描述
配置TableView,选择View下面的TableView鼠标右键,看到dataSource和delegata,按着confrol分别拖动到dataSource到File’s Owner进行关联,关联后就如上图所示。
2.TableViewController.h

#import <UIKit/UIKit.h>

@interface TableViewController : UIViewController<UITabBarDelegate,UITableViewDataSource>

@property(nonatomic,retain)NSArray* list;

@end

3.TableViewController.m


#import "TableViewController.h"

@interface TableViewController ()
{

    __weak IBOutlet UIButton *BackButton;

}

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   NSArray *array=[[NSArray alloc]initWithObjects:@"c",@"java",@"c++",@"oc",@"Python",@"go",@"c#",@"js",@"javaweb",@"asp.net", nil];
    self.list = array;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(IBAction)back:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)viewDidUnload
{

    [super viewDidLoad];
    self.list=nil;

}


//返回总行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [self.list count];
}

//为每一行赋值
static NSString *SimpleTableIdentifier=@"SimpleTableIdentifier";
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if(cell==nil){//如果行元素为空的话 则新建一行
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
    }
    //取得当前行
    NSUInteger row=[indexPath row];
    //设置每一行要显示的值
    cell.textLabel.text=[_list objectAtIndex:row]; 
    return cell;
}

//设置点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //该方法响应列表中行的点击事件
    NSString *heroSelected=[_list objectAtIndex:indexPath.row];
    //indexPath.row得到选中的行号,提取出在数组中的内容。
    UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:@"语言" message:heroSelected delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [myAlertView show];
    //点击后弹出该对话框。
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

然后运行。运行结果如第一张图。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值