UITableView基础(一)

简介:
 hello 大家又见面啦,今天小编给大家讲解一下UITableView,这一块知识在我们实际项目中是最为常见的,是ios项目的根骨所在,希望大家能够足够重视,所以小编准备分几次内容给大家一一解析其中的难点,下面是最基础的内容,希望大家能有所收获。
import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource>
//绑定tableView控件,定义变量,方便对控件的代码操作
@property (strong, nonatomic) IBOutlet UITableView *table;
//为tableView上显示数据定义两个变量
@property(nonatomic,strong)NSArray *community;
@property(nonatomic,strong)NSArray *details;

@end



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize details;
@synthesize community;
- (void)viewDidLoad {
    [super viewDidLoad];
    //对定义的两个变量赋予初始值
   community=[NSArray arrayWithObjects:@"图灵工作社",@"梦翔工作社",@"南工电脑网",@"锋芒工作社", nil];
    details=[NSArray arrayWithObjects:@"图灵最牛,挤压群熊",@"不错的社团",@"不错的社团",@"不错的社团", nil];
    self.table.dataSource=self;
    //添加页眉和页脚,在这里小编分别加载的是图片
    self.table.tableHeaderView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"001.jpg"]];
    self.table.tableFooterView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"001.jpg"]];
}
//UITableView协议里面的方法,对每个UITableCell进行定制和写入内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *cellId=@"cellId";//静态NSString类型,目的是可以重用能添加UITaleCell到重用表格中,和方便从重用表格中调取。
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil) {//第一次没有表格行(UITableCell)时,定制表格行
        switch (indexPath.row%4) {
            case 0:
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];//第一种UITableCell风格
                break;
           case 1:
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];//第二种UITableCell风格
                break;
            case 2:
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];//第三种UITableCell风格
                break;
                case 3:
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellId];//第四种UITableCell风格
                break;
      <span style="color:#ff0000;">//注意看下下边的图片,比较这四种UITableCell的不同点!!!</span>        
        }
        }
    
    NSUInteger rowNo=indexPath.row;//调取每行UITableCell的索引进行付于初值
    cell.textLabel.text=[community objectAtIndex:rowNo];
    cell.imageView.image=[UIImage imageNamed:@"001.jpg"];
    cell.imageView.highlightedImage=[UIImage imageNamed:@"001.jpg"];
    cell.detailTextLabel.text=[details objectAtIndex:rowNo];
    return cell;
}
//UIDataSource协议里面的第二个方法:目的告诉系统一个分区里面有多少个表格行(注意是一个分区,不一定是总共有多少表格行)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return community.count;
}
//UIDataSource协议里面的第二个方法:目的告诉系统有几个分区(几块表格)显然这里返回的是一个分区表格
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
运行结果如下:

怎么样,这点简单的知识掌握了没有啊?大家要愉快的学习,发觉编程的乐趣,提高自己的效率!后续马上给大家讲解深层次的UITableView知识,希望小编的这点微薄知识能帮到你们!微笑再见再见再见


目录
相关文章
|
3月前
|
开发者 iOS开发
介绍 UITableView 和 UICollectionView,它们的区别是什么?
介绍 UITableView 和 UICollectionView,它们的区别是什么?
58 0
UITableView的创建
UITableView的创建
75 0
|
iOS开发 容器
(五)UITableView的用法一
(五)UITableView的用法一
160 0
UITableView使用中的一些刁专问题总结
tableview中cell的系统分隔线问题(分隔线顶满或者缩短)
UITableView使用中的一些刁专问题总结
|
缓存 JavaScript iOS开发
UITableView 组件化
源起 在 iOS 开发中,UITableView 可以说是最常用的控件。几行代码,实现对应方法,系统就会给你呈现一个 60 帧无比流畅的列表,让初学者成就感爆棚。然而随着开发的深入,我们就会慢慢觉察到当前的 UITableView 实现会有这样或那样的问题。
925 0