2017-01-18 2 views
-1

Я хочу, чтобы у таблицы tableview была динамическая метка или изображение. Я не хочу, чтобы цикл добавлял subview в представление содержимого ячейки tableview, потому что он будет медленно работать при прокрутке. Так может кто-нибудь мне помочь? Как это изображение:Как сделать ярлык или динамическое изображение в tableviewcell?

Like this image

Мой код; enter image description here

+1

Какой код у вас пытался? Пожалуйста, опубликуйте его! http://stackoverflow.com/help/mcve – dfd

+0

https://i.stack.imgur.com/fjdUj.png –

+0

Я пытаюсь это сделать, но он не гладкий –

ответ

0

Не зная контекст вашего кода, что-то, как это может работать:

Примечание Это будет работать на прошивкой < = 8

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 
    @IBOutlet weak var TableVC: UITableView! 

    let elements = ["item1 item2 item3","item1 item2", "item1"] 



    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.TableVC.delegate = self 
     self.TableVC.dataSource = self 


     self.TableVC.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 





    } 

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return elements.count 
    } 


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

     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 



     cell.textLabel?.numberOfLines = 0 



     cell.textLabel?.attributedText = addTextToAttribute(text: elements[indexPath.item]) 




     return cell 
    } 



    func addTextToAttribute(text : String) -> NSMutableAttributedString { 


     let mutString = NSMutableAttributedString() 

    let s = text.components(separatedBy: .whitespaces) 
     let attr: [String: AnyObject] = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20)] 

     for text in s { 


     mutString.append(NSAttributedString(string: "-" + text + "\n", attributes: attr)) 
     } 


     return mutString 
    } 

} 

enter image description here

Смежные вопросы