ios - 使用协议(protocol)在 swift 中编写自定义函数

标签 ios swift uitableview protocols

我正在使用一个框架,该框架有一些我想扩展/修改的委托(delegate)方法。

对于使用特定于框架的语言,我深表歉意,但我的问题不在于框架,而在于协议(protocol)和委托(delegate)的概念。

我对它是如何工作的了解很浅,所以如果你忽略框架并帮助我了解大局也没关系。

我的类头实现了以下协议(protocol):

class ConversationListViewController: ATLConversationListViewController,
ATLConversationListViewControllerDelegate, ATLConversationListViewControllerDataSource,
LYRQueryControllerDelegate {...}

在ATLConversationListViewController源文件中,存在这个函数:

- (void)configureCell:(UITableViewCell<ATLConversationPresenting> *)conversationCell atIndexPath:(NSIndexPath *)indexPath
{
    LYRConversation *conversation = [self.queryController objectAtIndexPath:indexPath];
    [conversationCell presentConversation:conversation];

    if (self.displaysAvatarItem) {
        if ([self.dataSource respondsToSelector:@selector(conversationListViewController:avatarItemForConversation:)]) {
            id<ATLAvatarItem> avatarItem = [self.dataSource conversationListViewController:self avatarItemForConversation:conversation];
            [conversationCell updateWithAvatarItem:avatarItem];
        } else {
           @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Conversation View Delegate must return an object conforming to the `ATLAvatarItem` protocol." userInfo:nil];
        }
    }
//other conditions for various other delegate methods
}

我可以像这样在我的 ConversationListViewController 类中实现这个 avatarItemForConversation 函数:

func conversationListViewController(conversationListViewController: 
ATLConversationListViewController!, avatarItemForConversation
 conversation: LYRConversation!) -> ATLAvatarItem! {

    //implementation goes here

    return user as? ATLParticipant
}

我的问题是,当我设置我的头像时,我需要访问特定的 UITableViewCell(它实现了 ATLConversationPresenting 协议(protocol))——否则我必须从我的数据库同步下载头像图像,因为我不能用委托(delegate)函数的默认参数。

所以我的问题很简单:如何扩展委托(delegate)函数中可用的信息?

在我的例子中,我可以使用对 UITableViewCell 的引用或当前对话的 indexPath。 我只需要一种方法将对话与其所属的单元格相关联。

最佳答案

我也在研究 Layer+Atlas Framework。我有解决方案

对于 ATLParticipant...

class ConversationParticipant: ConversationAvatarItem, ATLParticipant {

   var firstName: String!
   var lastName: String!
   var fullName: String!
   var participantIdentifier: String!

   override init() {
       super.init()
   }
}

对于 ATLAvatarItem...

class ConversationAvatarItem: NSObject, ATLAvatarItem {

    var avatarImageURL: NSURL!
    var avatarImage: UIImage!
    var avatarInitials: String!

    override init() {
      super.init()
    }  

}

如何使用

func conversationListViewController(conversationListViewController: ATLConversationListViewController!, avatarItemForConversation conversation: LYRConversation!) -> ATLAvatarItem! {

    let  lastUser  = ConversationAvatarItem()
    lastUser.avatarImage = UIImage(named: "ee.png")

    return lastUser

}

关于ios - 使用协议(protocol)在 swift 中编写自定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732379/

相关文章:

ios - OneSignal (iOS) - 未调用 didReceiveNotificationRequest

ios - ResearchKit:是否有可能从 ORKConsentReviewStep 获取选择变量

ios - 在 iOS 中使用核心数据时,如何为分段 TableView 构建sections数组?

ios - UITableView 重新加载数据很慢

ios - 在 Swift 2 中检测物理接触

ios - 如何使用 NSPredicate 过滤存储在 CoreData 中的数组?

ios - 如何在邮件应用程序地址中制作带标签的 UITextField?

ios - RxSwift 条件绑定(bind)

ios - 将 NSAttributedString 转换为 Data 进行存储

ios - UITableView:刷新完成后强制 "release tableview"