Tương tác Row Selection trong UITableView

PART 2: CHÚNG TA ĐÃ TẠO RA ĐƯỢC TABBLEVIEW NHƯNG CHƯA LÀM GÌ VỚI NÓ BÀI NÀY SẼ HƯỚNG DẪN TƯƠNG TÁC VỚI CELLTABLE BẮT SỰ KIỆN ROW:

1. 1 Xử lý Selection Table Row:

- chúng ta mở PROJect phần trước ra tiếp tục tableView:
-AppDelegate khuôn mẫu cấp quyền cho tableView hứng tương tác :
tableviewcell có thể nhận event nạp data
xét 2 hàm trong AppDelegate:
- TableView: willSelectRowAtIndexPath:
- tableView: didSelectRowAtIndexPath:
đây là 2 hàm cho phép lựa chọn các Row trong cell :
TableView: willSelectRowAtIndexPath: quay lại lựa chọn.
tableView: didSelectRowAtIndexPath:lựa chọn để tương tác tới một hoạt động tiếp theo.
-Demo với một message:
Screen Shot 2016-04-12 at 12.13.29

lựa chọn khi thành công didSelectRowAtIndexPath:

khi ta click vào bất kỳ một Row nào ngay sau đó sẽ thông báo message....
=>ở đây ta đã bắt được sự kiện cho tất cả các ROW.
Xem thêm: Tìm hiểu học lâp trình android ở đâu tốt
1.2 demo app bắt sự kiện cho từng ROW :
- Ta comen phần thông báo trên lại :
- Trong hàm đó ta khai báo :
cellitem *cell=[tableView cellForRowAtIndexPath:indexPath];
=> lấy chỉ số IndexPath của tableView để lựa chọn ROW nào tương tác:
cụ thể : indexPath lưu địa chỉ và length , truyền cho cellForRowAtIndexPath xác định được địa chỉ cell .
trong cell có các type :
cell.accessoryType=UITableViewCellAccessoryNone. không có gì.
cell.accessoryType=UITableViewCellAccessoryDetailButton chi tiết
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton; chi tiết view mới
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator; chuyển sang view mới
cell.accessoryType =UITableViewCellAccessoryCheckmark; checkmark
// Demo chương trình :

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

cellitem *cell=[tableView cellForRowAtIndexPath:indexPath];

// cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton

if (cell.accessoryType==UITableViewCellAccessoryCheckmark) {// nếu đã check thì bỏ check

cell.accessoryType=UITableViewCellAccessoryNone;

}else{

cell.accessoryType =UITableViewCellAccessoryCheckmark;// check

}

}


Screen Shot 2016-04-12 at 14.14.50
ok !!!
2. Kết nối với một viewcontroller mới :
kéo viewcontroller vào viewcontroller mới chọn "show"
Screen Shot 2016-04-12 at 14.20.56

tạo identifine cho quan hệ :
Screen Shot 2016-04-12 at 14.21.14

đặt tên là "data"
đổi color background :
Screen Shot 2016-04-12 at 14.26.53
ok thiết kế xong giao diện:
thêm đoạn mã

if ([self.item[indexPath.row] isEqualToString:@"Na"]) {

[self performSegueWithIdentifier:@"data" sender:nil];

}

vào trong
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
khi lựa chọn "Na" chuyển đến view mới thành công ! điều hướng view...
Biên dịch và chạy thử
Screen Shot 2016-04-29 at 11.44.25
Gợi ý xem thêm: