天天看點

第四篇 UITableView建立項目效果1效果2

UITableView是非常常用的一個UI元件,這裡介紹其基本使用。

建立項目

建立一個單視圖項目,拖兩個VIEW到主界面。上面是一個高度為20的UIView視圖,下面是一個UITableView視圖。并進行簡單的自動布局設定。再示範一次吧:

第四篇 UITableView建立項目效果1效果2

接下來,我要實作兩個簡單效果:

效果1

第四篇 UITableView建立項目效果1效果2

效果2

第四篇 UITableView建立項目效果1效果2

1、添加表格項圖示

第四篇 UITableView建立項目效果1效果2

2、關聯表格對象

第四篇 UITableView建立項目效果1效果2

3、添加表格委托

第四篇 UITableView建立項目效果1效果2

接下來,上代碼了:

#define  DATASOURCE1

#import "ViewController.h"

@interface tableItem : NSObject

@property(nonatomic,strong) NSString* title;
@property(nonatomic,strong) UIImage* image;
@property(nonatomic,strong) NSString* des;

@end

@implementation tableItem
@end

@interface ViewController ()
{
    NSArray* datas1;
    NSMutableArray* datas2;
}
@property (weak, nonatomic) IBOutlet UITableView *table;

@end
           
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    datas1 = [NSArray arrayWithObjects:@"item1",@"item2",@"item3",@"item4",@"item5", nil];

    tableItem * ti;
    datas2 = [[NSMutableArray alloc] init];
    for (NSInteger i = ; i < ;  i++) {
        ti = [[tableItem alloc] init];
        ti.title = [@"item" stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)i]];
        ti.image = [UIImage imageNamed:@"tableIcon"];
        ti.des = [@"這是item" stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)i]];
        [datas2 addObject:ti];
    }
}

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

#pragma - tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#ifdef DATASOURCE1
   return  [datas1 count];
#else
   return  [datas2 count];
#endif
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellReuseID";
    UITableViewCell *cell;

    cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        #ifdef DATASOURCE1
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        #else
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        #endif
    }

#ifdef DATASOURCE1
    cell.textLabel.text = [datas1 objectAtIndex:indexPath.row];
#else
    tableItem* ti = [datas2 objectAtIndex:indexPath.row];
    cell.imageView.image = ti.image;
    cell.textLabel.text = ti.title;
    cell.detailTextLabel.text = ti.des;
#endif

    return cell;
}

#pragma - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
           

本篇知識點回顧:

1、複習UI元件關聯對象;

2、複習UI元件簡單布局;

3、NSArray和NSMutableArray的使用;

4、StoryBoard中,為UITableView添加委托;

5、UITableView幾個常用的委托方法;

6、UITableView的兩種簡單内容顯示方式;

7、圖示資源的添加。