5

Đây là một câu hỏi đơn giản mà tôi nghĩ sẽ có câu trả lời dễ tìm nhưng không có. Tôi muốn chọn một ô trong một collectionview. Vấn đề chính là tôi không thể đính kèm một bộ nhận dạng cử chỉ vào một ô mẫu. Tôi muốn lấy văn bản từ một nhãn trên ô được chạm vào. Tôi sử dụng tên trong một chức năng khác trong quan điểm của tôi.Làm cách nào để có tên của một bộ sưu tập đã chọn?

Hoặc một câu hỏi đơn giản hơn: Có hướng dẫn về lựa chọn nhấn từ danh sách các mục không?

Trả lời

6

Bạn có phương thức collectionView:didSelectItemAtIndexPath: trong đại biểu. Điều này sẽ kích hoạt khi bạn thu thập ô và cung cấp cho bạn indexPath chính xác cho ô cụ thể đó.

Sử dụng chỉ mục nàyPath kết hợp với bộ sưu tập của ViewView cellForItemAtIndexPath: để truy cập vào một ô cụ thể.

Ví dụ:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    [self manipulateCellAtIndexPath:indexPath]; 
} 

-(void) manipulateCellAtIndexPath:(NSIndexPath*)indexPath { 
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; 
    // Now do what you want... 
} 

Và, miễn là tôi đang ở đây. Phiên bản Swift:

override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) { 
    manipulateCellAtIndexPath(indexPath) 
} 

func manipulateCellAtIndexPath(indexPath: NSIndexPath) { 
    if let cell = collectionView?.cellForItemAtIndexPath(indexPath) { 
     // manipulate cell 
    } 
} 
Các vấn đề liên quan