2017-12-18 7 views
1

사용 URL.schemeUITableViewCell 클래스의 UITextView에서 해시 태그를 사용할 수 있지만 두 가지 문제가 있습니다.UITextView에서 UITableViewCell 클래스의 hashtag을 탭한 후 segue 시도

처음으로 세포 등급에서 segue하는 방법. 나는 perform segue 기능이 없습니다. UITableView 클래스에만 있습니다.

두 번째 해시 태그 이름 또는 언급 이름을 새로운 UIViewController에 보내는 방법. 위임 메서드를 사용하거나 segue 수행을 통해 보낼 수 있습니다. 그러나 다시 어떻게 세포 계급에서 나눌 것인가.

다음 코드는이 일을 여러 가지 방법이 있습니다

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 

    let path = URL.absoluteString 
    switch URL.scheme! { 
    case "hash" : 
     let hash = path.removingPercentEncoding?.components(separatedBy: ":").last 
     print(hash!) // ---> Retriving tapped on hash name correctly 

    case "mention" : 
     let mention = path.removingPercentEncoding?.components(separatedBy: ":").last 
     print(mention!) // ---> Retriving tapped on mention name correctly 
    default: 
     print("Just a regular link \(path.removingPercentEncoding!)") 
    } 
    return true 
} 

답변

0

UITableViewCell 클래스로 작성된 것입니다,하지만 난 아마 사용자 정의 위임 프로토콜을 사용합니다. 다음과 같이 셀 파일의 프로토콜을 정의합니다

protocol MyTableViewCellDelegate: class { 
    func myTableViewCell(_ cell: MyTableViewCell, shouldSelectHashTag tag: String) 
} 

테이블 뷰 셀 클래스에 속성을 추가

class MyTableViewCell: UITableViewCell { 
    // make sure the property is `weak` 
    weak var delegate: MyTableViewCellDelegate? 
} 

을 나는 테이블 뷰 데이터 소스 있으리라 믿고있어도에서보기 컨트롤러 너는 segue을하고 싶다.

extension MyViewController: MyTableViewCellDelegate { 
    func myTableViewCell(_ cell: MyTableViewCell, shouldSelectHashTag tag: String) { 
     performSegue(withIdentifier: "MyHashTagSegue", sender: tag) 
    } 
} 

cellForRowAtIndexPath 데이터 소스 방식 내부의 테이블 뷰 셀의 대리인으로보기 컨트롤러 할당 :

let cell: MyTableViewCell = <dequeue the cell> 
cell.delegate = self 

마지막을하는 것을 잊지 마세요이보기 컨트롤러가 새로운 프로토콜을 준수 확인 테이블 뷰 셀에서 대리자 메서드를 호출하십시오.

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 
    let tag = <get the hash tag> 
    delegate?.myTableViewCell(self, shouldSelectHashTag: tag) 
} 
+0

위대한 답변과 나는 그것이 효과가 있다고 생각합니다. 하지만 내 ViewController는 내가 이야기하려고하는 스토리 보드와는 다른 스토리 보드에 있습니다. 어떻게이 문제를 해결할 수 있다고 생각하니?

+0

스토리 보드 참조를 사용할 수 있습니다. 이것은 뷰 컨트롤러처럼 스토리 보드로 드래그 할 수있는 또 다른 객체이며, 뷰어 컨트롤러에 세그 (segue)를 연결할 수 있습니다. – daltonclaybrook

+0

예 이것은 내가 선택한 스토리 보드에 instantiateViewController를 한 것입니다. 감사 :)) –

관련 문제