简单的表格

来源:互联网 发布:二级域名 端口号 编辑:程序博客网 时间:2024/06/05 02:52


#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>//让类遵循两个协议,类只有遵循这两个协议才能成为表视图的委托和数据源


@property (copy,nonatomic)NSArray *dwarves;


@end


/*

                    实现一个简单表的步骤:

             1、这是一个极为简单的应用,不需要任何的输出接口或操作方法。从库中拖出一个 Table View

             2、让拖出来的表视图占满整个屏幕,注意顶部要放在状态栏下方看到的一条蓝色的引导线

             3Pin固定)边缘与父视图之间的距离。如果采用约束的话,将 Update Frames设为 Items of New Constraints

             4、选中表视图,打开关联检查器,把 dataSource数据源 delegate委托)关联到 View Controller

             5、添加一个图像。    是不是需要创建一个 UITableViewCell子类或使用子视图添加图像?不用。实际上,如果能接受图像位于每一行的左侧的话,就不需要做额外的工作了。 (添加两张png图片到 Images.assets

 

 

 

 

 

 

 

 

 */


@implementation ViewController


//根据行序来设置每一行的缩进级别。所以第0行的缩进级别为0,第1行的缩进级别为1,以此类推。由于存在 %操作符,所以第 4行将回到缩进级别0并继续循环。缩进级别是一个整数,他会告诉表视图把行向右移动一点。缩进级别的数值越大,向右缩进的就越多。

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(nonnullNSIndexPath *)indexPath {

    return indexPath.row %4;

}











- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.(加载视图之后(通常从 nib文件),进行一些额外设置 )

    //创建了一个数据数组,这些数据要在表中显示   在实际的应用中,数据应该来自其他源,例如文本文件、属性文件或者 wed服务

    self.dwarves =@[@"Sleepy",@"Sneezy",@"Bashful",@"Happy",

                     @"Doc",@"Grumpy",@"Dopey",

                     @"Thorin",@"Dorin",@"Nori",@"Ori",

                     @"Balin",@"Dwalin",@"Fili",@"Kili",

                     @"Oin",@"Gloin",@"Bifur",@"Bofur",

                     @"Bombur"];

    

}












- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}










//让表获取指定分区的行数默认的分区号为 1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [self.dwarvescount];

}


//当视图需要绘制其中一行时,会调用此方法                       第一个参数 tableView是一个引用,指向当前构建的表(这样就可与你创建一个类来充当多个表的数据源;第二个参数是一个 NSIndexPath实例,表视图正是使用 NSIndexPath结构体把分区和行的索引绑定到一个对象中(要从NSIndexPath中获得行或分区的索引,只需要使用 row属性或 section属性即可,这两个属性都返回一个整型值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnullNSIndexPath *)indexPath {

    //声明一个静态的字符串,此字符串将作为键来使用,用来表示某种表单元

    staticNSString *SimpleTableIdentifier =@"SimplerTableIdentifier";

    

    //创建一个 SimplerTableIdentifier类型的可重用单元        当表视图单元滚离屏幕时,它们将被放置在一个可重用的单元队列中,如果系统运行较慢,表视图就从队列中删除这些单元,以释放储存空间。不过,只要有可用的储存空间,队列会一直保存这些单元,以便日后再次使用。每当表视图单元滚离屏幕时,另一个表视图单元就会从另外一遍滚动到屏幕上。如果滚动到频道上的新行重新使用滚离屏幕的一个单元,系统就能避免不断创建和释放哪些视图的开销。))

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    //判断 cell是否为 nil

    if (cell  ==nil) {

        //如果是,则使用相同的标识符字符串手动创建一个新的表视图单元。    从某种程度上来说,我们将不可避免地重复使用此处创建的单元,因此需要确保它是使用 SimplerTableIdentifier创建的。)

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault

                     /*

                        UITableViewCellStyleDefault ( 默认单元格式 )

                        UITableViewCellStyleSubtitle ( 单元格样式字幕 )对于子标题样式,两个文本元素都会显示,其中一个位于另一个下方

                        UITableViewCellStyleValue1 ( 单元格样式值 1 )此样式将文本标签和详细文本标签放在同一行,分别位于单元格的两端

                        UITableViewCellStyleValue2 (单元格样式值2 )这个样式并不显示表视图单元的图标,而是将详细文本标签放在文本标签的左侧。这种格式通常用于在信息旁边显示一个描速性的标签(在此布局中,详细文本标签用于描述文本标签中保存的数据类型

                      */

                                      reuseIdentifier:SimpleTableIdentifier];

    }

    //为每张图片创建一个 UIimage并在表视图请求每一个行的数据源时将其赋值给 UITableViewCell          image属性的图像出现在单元的文本的左侧,并且在选中单元时会被替换为 highlightedImage

    UIImage *image = [UIImageimageNamed:@"star"];

    cell.imageView.image = image;

    UIImage *highlightedImage = [UIImageimageNamed:@"star2"];

    cell.imageView.highlightedImage = highlightedImage;

    

    //使用表的行号从数组中获取相应的字符串,然后将其赋给表单元的 textLabel.text属性

    cell.textLabel.text =self.dwarves[indexPath.row];

    //更改表视图中字体的大小

    //cell.textLabel.font = [UIFont boldSystemFontOfSize:50];

    //也可以首先告诉表,所有的行都应该指定一个固定的高度

    //tableView.rowHeight = 70;

    

    if (indexPath.row <7) {

        cell.detailTextLabel.text =@"Mr. Disney";

    } else {

        cell.detailTextLabel.text =@"Mr. Tolkien";

    }

    //最后将表单元返回即可

    return cell;                                                                                                                                                                                                                                                                                                                                             

}




//让不的行拥有不同的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnullNSIndexPath *)indexPath {

    //除来第一行要高一些外,表视图把所有行高斗设置为 70

    return indexPath.row ==0 ? 120 :70;

}





//指定不能选中第一行

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(nonnullNSIndexPath *)indexPath {

    //判断 indexPath的索引是否为 0

    if (indexPath.row ==0) {

        //如果是就返回 nil,表示实际上不应该选中其中任何一行

        returnnil;

    }//选中偶数行时,实际选中的是它下面的那一行        如果想改变选中的行或分区表,可以使用NSIndexPath indexPathForRow: inSection: 方法来创建一个新的 NSIndexPath对象并返回它

    else if (indexPath.row %2 ==0) {

        return [NSIndexPathindexPathForRow:indexPath.row +1 inSection:indexPath.section];

    }//否则,它返回原来的 indexPath,表示可以继续选中 indexPath对应的行

    else {

        return indexPath;

    }

}





//创建一个弹出警告视图,用于显示被选中的行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnullNSIndexPath *)indexPath {

    // dwarves的索引路径赋值给 rowValue字符串

    NSString *rowValue =self.dwarves[indexPath.row];

    //为每一行创建一个点击后显示的警告视图内容

    NSString *message = [[NSStringalloc] initWithFormat:@"You selected %@",rowValue];

    

    //创建警告视图的上方的标题名称

    UIAlertController *controller =

             [UIAlertControlleralertControllerWithTitle:@"Row Selected"

                                                       message:message

                                                       preferredStyle:UIAlertControllerStyleAlert];

    //创建一个取消的标题内容

    UIAlertAction *cancelAction =

    [UIAlertActionactionWithTitle:@"Yes I Did"

                             style:UIAlertActionStyleDefault

                           handler:nil];

    [controller addAction:cancelAction];

    [selfpresentViewController:controlleranimated:YEScompletion:nil];

    

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

}


@end


运行结果如下: