Tải bản đầy đủ (.pdf) (7 trang)

TƯƠNG tác ROW SELECTION TRONG UITABLEVIEW

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (387.94 KB, 7 trang )

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:


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 :
1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
2.
3. cellitem *cell=[tableView cellForRowAtIndexPath:indexPath];
4.
5. // cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton
6.
7. if (cell.accessoryType==UITableViewCellAccessoryCheckmark) {// nếu đã check thì bỏ check


8.
9.
10.
11.
12.
13.
14.
15.
16.


cell.accessoryType=UITableViewCellAccessoryNone;
}else{
cell.accessoryType =UITableViewCellAccessoryCheckmark;// check
}

17. }

ok !!!
2. Kết nối với một viewcontroller mới :
kéo viewcontroller vào viewcontroller mới chọn “show”


tạo identifine cho quan hệ :


đặt tên là “data”
đổi color background :


ok thiết kế xong giao diện:
thêm đoạn mã
1. if ([self.item[indexPath.row] isEqualToString:@"Na"]) {
2.
3. [self performSegueWithIdentifier:@"data" sender:nil];
4.
5. }

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ử



×