void函数/ swift中意外的非无效返回值

时间:2019-02-25 12:02:09

标签: ios swift xcode

extension VoiceController: UITableViewDataSource, UITableViewDelegate {
    public func tableView(_ chatHistory: UITableView, numberOfRowsInSection section: Int) -> Int {
        return userMessagesData.count
    }
    public func tableView(_ chatHistory: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        func userMessage() -> UITableViewCell {
            let userCell = chatHistory.dequeueReusableCell(withIdentifier: "userMessage")! as! UITableViewCell
            userCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
            userCell.textLabel!.numberOfLines = 0
            userCell.textLabel!.lineBreakMode = .byWordWrapping
            userCell.textLabel!.text = userMessagesData[indexPath.row]
            userCell.textLabel!.textAlignment = .right
            return userCell;
        }
        func botMessage() -> UITableViewCell {
            let botCell = chatHistory.dequeueReusableCell(withIdentifier: "botMessage")! as! UITableViewCell
            botCell.textLabel!.textColor = UIColor(red:1.00, green:0.56, blue:0.25, alpha:1.0)
            botCell.textLabel!.numberOfLines = 0
            botCell.textLabel!.lineBreakMode = .byWordWrapping
            botCell.textLabel!.text = botMessagesData[indexPath.row]
            botCell.textLabel!.textAlignment = .left
            return botCell;
        }
    }
}

那是我的代码。我需要做一些工作以使此代码起作用。如果我删除其中一项功能,那将起作用 我不明白,如何更改我的代码才能工作。请帮助

2 个答案:

答案 0 :(得分:2)

您需要

public func tableView(_ chatHistory: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let item = arr[indexPath.row]

    if item.isUser {

        let userCell = chatHistory.dequeueReusableCell(withIdentifier: "userMessage")! as UITableViewCell
        userCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
        userCell.textLabel!.numberOfLines = 0
        userCell.textLabel!.lineBreakMode = .byWordWrapping
        userCell.textLabel!.text = item.messsage
        userCell.textLabel!.textAlignment = .right
        return userCell
    }
    else {

        let botCell = chatHistory.dequeueReusableCell(withIdentifier: "botMessage")! as UITableViewCell
        botCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
        botCell.textLabel!.numberOfLines = 0
        botCell.textLabel!.lineBreakMode = .byWordWrapping
        botCell.textLabel!.text = item.message
        botCell.textLabel!.textAlignment = .left
        return botCell
    }
}

struct Item { 
  let isUser:Bool
  let message:String 
}

arr

var arr = [Item]()

答案 1 :(得分:0)

您需要返回在函数中声明的相同返回类型,

func botMessage() -> Void //returns void 

返回UITableViewCell时。将Void替换为UITableViewCell

我还注意到您有返回Int的子函数,删除该函数是不必要的。