2015-06-15 19 views
6

Mam problem z moim TableViewCellTableViewCell nakłada się na tekst

Mam dwa typy komórek w moim scenopisie. , gdy przewijam, tekst nakłada się w niektórych komórkach. Próbuję wszystkiego, ale nie wiem jak inaczej. bardzo dziękuję za pomoc

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 

     var storeNew = systemsBlog.getStore(listNews[indexPath.row].getIdStore()) 
     var newNotice = listNews[indexPath.row] 

     let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) as? TimelineCell 

       cell!.nameLabel.text = storeNew.getName() 
       cell!.postLabel?.text = newNotice.getText() 
       cell!.postLabel?.numberOfLines = 0 
       cell!.dateLabel.text = newNotice.getDate() 
       cell!.typeImageView?.tag = indexPath.row; 
       return cell! 
} 



class TimelineCell : UITableViewCell { 

    @IBOutlet var nameLabel : UILabel! 
    @IBOutlet var postLabel : UILabel? 
    @IBOutlet var dateLabel : UILabel! 

    override func awakeFromNib() { 
    postLabel?.font = UIFont(name: "Roboto-Thin", size: 14) 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 
    } 

enter image description here

+0

widzę TimelineCell gdzie jest inny typ? – Icaro

+0

Tego rodzaju rzeczy mogą się zdarzyć podczas ponownego użycia komórek i ciągłego dodawania subviews w komórce dla metody wiersza. Jednak nie widzę tego w kodzie, który opublikowałeś. Czy jest więcej kodu? –

+0

@lcaro, innego typu nie używam, ponieważ teraz próbuję rozwiązać ten problem. – Carol

Odpowiedz

5

Mogę naprawić problem. W scenorysie etykieta ma niezakodowane "Czyść kontekst graficzny". Sprawdziłem i na razie zostało to rozwiązane! Dzięki za pomoc!

+0

Próbowałem wielu hacków. tylko ta prosta rzecz pomogła. Dzięki. – Bindi

0

miałem podobny problem z jednym z moich UITableViews w przeszłości. Istnieje wiele rzeczy, które mogą spowodować to, ale może to jest to samo, co mi się przydarzyło.

Widzę, że używasz niestandardowego tableViewCell. To, co może się zdarzyć, gdy ustawisz tekst komórki, dodaje widok etykiety z tym tekstem. Teraz powiedz, że przewijasz widok tabeli i ta komórka znika. Jeśli ponownie wykorzystasz tę komórkę i nie usuniesz etykiety z podglądu, lub ponownie ustawisz tekst tej etykiety na żądany tekst, użyjesz tableviewcell z poprzednią etykietą i dodasz nową etykietę z nowym tekst do niego, pokrywający się z tekstem.

Moja sugestia polegałaby na tym, aby nie dodawać UIlabels jako subviews w klasie TimelineCell, chyba że nie istnieje żadna etykieta. jeśli istnieje etykieta, edytuj tekst tej etykiety, a nie komórki.

+0

Cześć, Tyler. Nie dodaję etykiety do komórki. Mam etykietę na storyboardzie i ustawiam tylko tekst. – Carol

0

Zgodnie apple documentation:

realizacja widoku tabeli za źródło danych Tableview: cellForRowAtIndexPath: Należy zawsze przywrócić całą zawartość gdy ponowne komórkę.

Wydaje się, że jest problem, że nie zawsze ustawienie postLabel, powodując jej pisać na szczycie innych komórek, spróbuj tego:

//reuse postLabel and set to blank it no value is returned by the function  
let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) as? TimelineCell 

      cell!.nameLabel.text = storeNew.getName() 
      if newNotice.getText() != nil{ 
       cell!.postLabel.text = newNotice.getText() 
      } else {cell!.postLabel.text = ''} 
      cell!.postLabel.numberOfLines = 0 
      cell!.dateLabel.text = newNotice.getDate() 
      cell!.typeImageView?.tag = indexPath.row; 
      return cell! 
} 

//Make postLabel mandatory and set the font details in the xcode 
class TimelineCell : UITableViewCell { 
@IBOutlet var nameLabel : UILabel! 
@IBOutlet var postLabel : UILabel! 
@IBOutlet var dateLabel : UILabel! 

override func awakeFromNib() { 
//set this in xcode 
} 

override func layoutSubviews() { 
    super.layoutSubviews() 
} 

także mieć pewność, że nie stwarzają żadnego UI element i dołączanie do komórki, tak jakbyś musiał ją wyrzucić przed ponownym przetworzeniem komórki.

+0

Dzięki Icaro, ale nadal mam problem. – Carol

0

Można spróbować ustawić:

override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView.rowHeight = UITableViewAutomaticDimension 
     self.tableView.estimatedRowHeight = 44.0 // Set this value as a good estimation according to your cells 
} 

w widoku kontrolera zawierającego swoją tableView.

Upewnij ograniczenia układu w twojej TimelineCell zdefiniować wyraźny linię ograniczeń wysokościowych

Inną opcją jest odpowiedzią na:

tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
return 44.0 // Your height depending on the indexPath 
} 

zawsze w ViewController który zawiera tableView i, przypuszczam , jest tableView 's UITableViewDelegate Mamy nadzieję, że to pomoże

0

Ustaw komórkę na zero, która naprawi jakiś błąd. func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? ImageCellTableViewCell 
    cell = nil 
    if cell == nil { 
     cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath) as? ImageCellTableViewCell 
    } 
    cell.yourcoustomtextTextLabel.text = "this is text" 
    cell.yourcoustomtextImageView.image = image 
    return cell 
} 
Powiązane problemy