HƯỚNG DẪN SỬ DỤNG TABLEVIEW TRONG IOS với việc tạo được ứng dụng hello World chúng ta đã nắm bắt cơ bản về message bắt sự kiện button kết nối file tiếp theo chúng ta sẽ làm việc với tabl
Trang 1HƯỚNG DẪN SỬ DỤNG TABLEVIEW TRONG IOS
với việc tạo được ứng dụng hello World chúng ta đã nắm bắt cơ bản về message bắt
sự kiện button kết nối file tiếp theo chúng ta sẽ làm việc với tableView trong ios
tableView được thiết kế rất nhiều trên ios List Bài hát, list món ăn, setting trong ios…
bài này mình sẽ hướng dẫn cụ thể cho các bạn về tableView! chúng ta bắt đầu nào! ! !
bước 1- CREATE PROJECT MỚI
-vào Main.storyboard
-kéo vào tableView
Trang 4-Kéo vào tableviewcell
Trang 5
-kéo vào imageView và label trong contenView
-table quản lý các cell -> trong cell chứa các contenView chúng ta tương tác trực tiếp với conTenView
-table có các hàm tạo sẵn AppDelegate khuôn mẫu để quản lý hứng sự kiên bắt data lên contenView
– Để làm được điều đó chúng ta kết nối Appdelegate
-thực hiện nối tableView với viewcontroller của chính nó uỷ quyền cho ViewController quản lý tableView qua :UITableViewDelegate,UITableViewDataSource
Trang 6– như thế ta đã tạo giao diện xong :
-ta xoá ViewController.h và m tạo 2 file mới tableViewController.h và m create file:
Trang 8sau đó chọn thư mục lưu ok!
-Kết nối
tableViewController với viewcontroller của nó!
Trang 9như vậy class tableViewController đã quản lý viewcontroller mà chúng ta tạo
Trang 10tạo identifine: để xác định xem đang làm cho section nào!
vào tableViewController.h
Trang 11kết nối :
1 @interface tableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
khởi tạo class quản lý Cell trong tableView sau đó kéo nôí image và label
1 @interface cellitem: UITableViewCell
2 @property( weak ,nonatomic ) IBOutlet UILabel *label;
3 @property ( weak ,nonatomic ) IBOutlet UIImageView* imageIcon;
-Quản lý tableView:
AppDelegate có 2 hàm bắt buộc
// đưa ra số lượng hàng trong nhóm section quản lý khuôn mẫu cell
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
};
//Hứng data vào cell tương tác dữ liệu
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}
– Chúng ta phải khởi tạo dữ liệu đưa vào cell…
cho ảnh vào resource
đặt tên
Trang 12@interface tableViewController ()
@property(nonatomic,strong) NSArray *item;//text
@property(nonatomic,strong) NSArray *linkUrl;/tên ảnh+ đôi
@end
trong viewdidload khởi tạo giá trị
1 - ( void ) viewDidLoad {
2 [super viewDidLoad ] ;
3 // Do any additional setup after loading the view
4 self item =@ [@"Na",@" Ổi ",@" Táo" ] ;
5 self linkUrl =@ [@"na jpg",@" oi jpg",@" tao jpg "
6 }
1 ( UITableViewCell * ) tableView: ( UITableView * ) tableView cellForRowAtIndexPath: ( NSIndexPath * ) indexPath {
2
3 return[ self item count ] ; //đưa ra số lượng cột
4 } - UITableViewCell * ) tableView: ( UITableView * ) tableView cellForRowAtIndexPath: ( NSIndexPath * ) indexPath {
5 cellitem *cell= [ tableView dequeueReusableCellWithIdentifier: @"cellitem " forIndexPath:indexPath ];//khởi tạo indentifine
6 cell label text =self item[ indexPath row];// lấy text đưa vào
7 cell imageIcon image = [ UIImage imageNamed:self linkUrl[ indexPath row]] ;/lấy tên ảnh đưa vào
8 return cell;
9
Trang 1310 }
ok !!! phần tiếp theo mình sẽ giúp các bạn tương tác với row và section trong tablewView!
link full mã nguồn!
1 http ://www.mediafire.com/download/hmvyip19pdqbhsr/tableViewController.zip