FUIAttachmentsFormCell

open class FUIAttachmentsFormCell: NibDesignableTableViewCell

  • The reusable UI component implemented as an UITableViewCell to manage selecting attachments. The application must provide a list of FUIAttachmentAction implementations for the desired type of attachments. The app can use the built-in types or implement additional types as desired.

  • Important

    the cell’s parent UITableViewController should subclass FUIFormTableViewController.

    There are two built-in FUIAttachmentAction: - FUIAddPhotoAttachmentAction: Choose photo from the photo library. - FUITakePhotoAttachmentAction: Take photo using the camera.

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCell(withIdentifier: FUIAttachmentsFormCell.reuseIdentifier, for: indexPath) as! FUIAttachmentsFormCell
         cell.attachmentsController.delegate = self
         cell.attachmentsController.dataSource = self
    
         let addPhotoAction = FUIAddPhotoAttachmentAction()
         addPhotoAction.delegate = self
         cell.attachmentsController.addAttachmentAction(addPhotoAction)
    
         let takePhotoAction = FUITakePhotoAttachmentAction()
         takePhotoAction.delegate = self
         cell.attachmentsController.addAttachmentAction(takePhotoAction)
    
         let customAction = CustomAction(controller: self)
         cell.attachmentsController.addAttachmentAction(customAction)
    
         return cell
     }
    
     var attachmentURLs: [URL] = [URL]()
    
    // MARK: FUIAttachmentsViewControllerDataSource methods
    func attachmentsViewController(_ attachmentsViewController: FUIAttachmentsViewController, iconForAttachmentAtIndex index: Int) -> (image: UIImage, contentMode: UIViewContentMode)? {
        let urlString = self.attachmentURLs[index].absoluteString
        guard let image = self.attachmentThumbnails[urlString] else {
            return nil
        }
        return (image!, .scaleAspectFill)
    }
    
    func numberOfAttachments(in attachmentsViewController: FUIAttachmentsViewController) -> Int {
        return attachmentURLs.count
    }
    
    func attachmentsViewController(_ attachmentsViewController: FUIAttachmentsViewController, urlForAttachmentAtIndex index: Int) -> URL? {
        return attachmentURLs[index]
    }
    
    // MARK:  FUIAttachmentsViewControllerDelegateMethods
    func attachmentsViewController(_ attachmentsViewController: FUIAttachmentsViewController, couldNotPresentAttachmentAtIndex index: Int) {
        // TODO:  present the attachment, using a compatible viewer
    }
    
    func attachmentsViewController(_ attachmentsViewController: FUIAttachmentsViewController, didPressDeleteAtIndex index: Int) {
        self.attachmentURLs.remove(at: index)
        self.tableView.reloadSections(IndexSet(integer:attachmentSection), with: .automatic)
    }
    
    
    //MARK: FUIAddPhotoAttachmentActionDelegate
    func addPhotoAttachmentAction(_ action: FUIAddPhotoAttachmentAction, didSelectPhotoAt url: URL) {
        self.addAttachmentURL(url)
    }
    
    //MARK: FUITakePhotoAttachmentActionDelegate
    
    func takePhotoAttachmentAction(_ action: FUITakePhotoAttachmentAction, didTakePhotoAt url: URL) {
        self.addAttachmentURL(url)
    }
    
    
    func addAttachmentURL(_ url: URL) {
        self.attachmentURLs.append(url)
        DispatchQueue.main.async {
            self.tableView.reloadSections(IndexSet(integer:self.attachmentSection), with: .automatic)
            self.tableView.scrollToRow(at: IndexPath(row: 0, section: self.attachmentSection) , at: .middle, animated: true)
        }
    }