IOS开发—UI表视图

#import <UIKit/UIKit.h>
// 1 签订tableview的两个协议
@interface MainViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *arr;

@end
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建表视图
    UITableView *tavleView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    // 设置tavleView
    // 宽度
    tavleView.rowHeight = 100;
    // 分割线样式
    tavleView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    // 分割线颜色
    tavleView.separatorColor = [UIColor cyanColor];
    
    // 将tavleView的代理人(delegate)指定为当前的viewController

    tavleView.dataSource = self;
    tavleView.delegate = self;
    [self.view addSubview:tavleView];
    [tavleView release];
}
// 每个section(区)里面有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"显示多少行");
    return [self.arr count];
    
}
// 每一行要显示什么样的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *string = @"顾佳炜";
    // 1.从重用池中获取一个cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    // 2.判断是否从重用池中取得cell
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
                 initWithStyle:
                 UITableViewCellStyleSubtitle
                 reuseIdentifier:string]
                autorelease];
        NSLog(@"创建cell");
    }
    // 3.重新对cell赋值
    // indexPath 有两个属性:section / row代表一个cell在tableView
    cell.textLabel.text = [NSString stringWithFormat:@"%d, %d", indexPath.section, indexPath.row];
    // 设置图片
    cell.imageView.image =[UIImage imageNamed:@"1.jpg"];
    // 副标题
    // 根据indexpath的row 取得素组里的相应元素
    cell.detailTextLabel.text = [self.arr objectAtIndex:indexPath.row];
    return cell;
    
}
// tableView的delegate方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值