Làm cách nào để phát hiện khi nào UITableView
đã được cuộn xuống dưới cùng để có thể nhìn thấy ô cuối cùng?UITableView Phát hiện tế bào cuối
Trả lời
Triển khai phương thức tableView:willDisplayCell:forRowAtIndexPath:
trong UITableViewDelegate của bạn và kiểm tra xem đó có phải là hàng cuối cùng không.
Bên tableView:cellForRowAtIndexPath:
hoặc tableView:willDisplayCell:forRowAtIndexPath:
như thế này:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSInteger sectionsAmount = [tableView numberOfSections];
NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]];
if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
// This is the last cell in the table
}
...
}
bạn có thể cũng đã nói tôi là một giả. cảm ơn vì sự giúp đỡ đôi khi tôi nghĩ những điều này khó hơn họ. – Ward
numberOfSections gọi số nguồn dữ liệuOfSectionsInTableXem và numberOfRowsInSection số nguồn dữ liệu cuộc gọiOfRowsInSection. Và trong thực hiện của tôi nó đã bị rơi cho thấy đăng nhập đệ quy này thực hiện. –
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
// This is the last cell
}
}
Đối với Xcode 8 và nhanh chóng 3
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
print("this is the last cell")
}
}
Đối với Xcode 8 và Swift3
//Show Last Cell (for Table View)
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
if indexPath.row == (yourdataArray.count - 1)
{
print("came to last row")
}
}
//Show last cell (for Collection View)
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)
{
if indexPath.row == (yourdataArray.count - 1)
{
// Last cell is visible
}
}
Tạo mở rộng thanh lịch cho UITableView
:
extension UITableView {
func isLast(for indexPath: IndexPath) -> Bool {
let indexOfLastSection = numberOfSections > 0 ? numberOfSections - 1 : 0
let indexOfLastRowInLastSection = numberOfRows(inSection: indexOfLastSection) - 1
return indexPath.section == indexOfLastSection && indexPath.row == indexOfLastRowInLastSection
}
}
Ví dụ về sử dụng:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.isLast(for: indexPath) {
//do anything then
}
return cell
}
tôi đã có một số vấn đề với phương pháp willDisplayCell. Điều này phù hợp với tôi:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let y = scrollView.contentOffset.y/(scrollView.contentSize.height - scrollView.frame.size.height)
let relativeHeight = 1 - (table.rowHeight/(scrollView.contentSize.height - scrollView.frame.size.height))
if y >= relativeHeight{
print("last cell is visible")
}
}
Chỉ cần chuyển điều này theo phương pháp đại biểu và thay đổi 'bảng' thành bảng của bạnXem.
- 1. Set dữ liệu cho các tế bào UITableView tĩnh
- 2. Thêm thụt lề trái để UITableView tế bào
- 3. UITableView dequeued tế bào tải lại hình ảnh cũ
- 4. Chiều cao tế bào UITableView động Dựa trên nội dung
- 5. Tai nạn tế bào nguyên mẫu UISearchDisplayController và UITableView
- 6. Phát hiện cuộn UITableView
- 7. làm thế nào biết nếu uitableview có một tế bào được chọn
- 8. Phát hiện các thay đổi của tế bào trong angular.js và ng-lưới
- 9. thực hiện chỉnh sửa tế bào jqgrid datepicker
- 10. Làm thế nào để loại bỏ các tế bào từ UITableView trực tiếp?
- 11. UITableView tiêu đề nhấp nháy khi chúng ta tái sử dụng định danh tế bào
- 12. Vấn đề làm mới tế bào UITableView về sắp xếp lại
- 13. MonoTouch - Phát hiện cuộn UITableView
- 14. Excel: Set màu nền của tế bào để giá trị rgb dữ liệu trong tế bào
- 15. Preallocation của mảng tế bào trong MATLAB
- 16. Bọc tế bào bằng vb.net & EPPlus
- 17. iOS: tế bào cellForRowAtIndexPath đang nhận được lẫn lộn
- 18. Lấy tế bào không trống cuối cùng trong một cột trong Google Sheets
- 19. tế bào và phương pháp có liên quan
- 20. Biến đổi tế bào bảng Angularjs
- 21. DataGridView với vấn đề tế bào CheckBox
- 22. Orgmode Bảng Formatting của tế bào
- 23. Nhận giá trị từ 3 tế bào không trống cuối cùng trong một hàng
- 24. UITableViewCell - chồng chéo với nội dung các tế bào trước
- 25. Sử dụng dequeueReusableCellWithIdentifier cho tế bào tùy chỉnh
- 26. làm thế nào để phát hiện một sự thay đổi trong bất kỳ tế bào excel trong C#?
- 27. LibreOffice/OpenOffice Calc - thêm chuỗi các tế bào
- 28. Làm thế nào để có được văn bản tế bào dựa trên indexPath?
- 29. Nhiều dòng trong một tế bào của một bảng
- 30. tế bào Storyboard tĩnh: dequeueReusableCellWithIdentifier trả về nil
cảm ơn mẹo. Tôi đang thực sự sử dụng phương pháp này để kích hoạt một sự kiện và có vẻ như nó là "Will" hiển thị, nó xảy ra trước khi nó thực sự hiển thị các tế bào. Có phương pháp nào phát sinh sau khi ô được hiển thị không? – Ward
Than ôi, không. Bạn có thể fudge một chút bằng cách sử dụng 'performSelector: withObject: afterDelay' với một sự chậm trễ rất ngắn để gọi một phương thức làm những gì bạn cần. –
@ArtGillespie Tìm thấy chính xác những gì tôi đang tìm kiếm. Chúc mừng! –