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:
- 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 :
// 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
}
}
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ã
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ử
Gợi ý xem thêm:
- Bộ tài liệu giúp bạn tu hoc lap trinh android
- Cách thức tự học lập trình ios
ConversionConversion EmoticonEmoticon