tableView 简单使用

来源:互联网 发布:牛津简明英语词典 知乎 编辑:程序博客网 时间:2024/05/22 05:21
////  ViewController.m//  test////  Created by 王小胜 on 2017/2/27.//  Copyright © 2017年 王小胜. All rights reserved.//#import "ViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor grayColor];        UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];        tableView.dataSource = self;    tableView.delegate = self;        [self.view addSubview:tableView];}#pragma - mark cell 的点击代理方法-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            }];        [alert addAction:ok];        [self.navigationController presentViewController:alert animated:YES completion:nil];        NSLog(@"点击第%ld个cell",(long)indexPath.section);    }#pragma - mark dateSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return 1;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {    return 10;}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {    return 2;}- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {    return 2;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    return 100;}////设置头名字//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {//    return @"测试头";//}//////设置尾名字//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{//    return @"测试尾巴";//}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [[UITableViewCell alloc]init];    cell.textLabel.text = @"测试";    cell.imageView.image = [UIImage imageNamed:@"test"];        UITextField *textFiled = [[UITextField alloc]initWithFrame:CGRectMake(150, 2, 100, 40)];    textFiled.borderStyle = UITextBorderStyleRoundedRect;        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 42, self.view.bounds.size.width, 20)];    [cell addSubview:view];    [cell addSubview:textFiled];        return cell;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

0 0
原创粉丝点击