2016-08-02 4 views
0

Ich weiß, dass die Lösung ziemlich einfach sein kann, aber ich habe Probleme mit der Positionierung dieser Ansichten.Monotouch - Verwenden Sie eine for-Schleife, um UILabels programmgesteuert untereinander zu erstellen

Ich versuche UILabels dynamisch zu erstellen, innerhalb einer UITableViewCell. Die Nummer UILabels ist zur Kompilierzeit unbekannt.

Basierend auf der Größe meiner Liste von Daten, möchte ich eine neue UILabel, eine untereinander erstellen und die Daten bei jedem Index in der Liste, auf der entsprechenden UILabel.

Da diese in einem UITableViewCell erstellt werden, möchte ich, dass sie unter der Zelle TextLabel positioniert werden.

Derzeit sind meine neuen UILabels alle übereinander gestapelt, über der TextLabel und ich kann nicht scheinen, sie richtig zu positionieren.

public void UpdateCell(List<Records> allRecords,List<string> otherInfo) 
    { 
     try{ 

      UILabel label = new UILabel(); 

      myRecords = allRecords; 

      for(int i = 0; i < myRecords.Count;i++) 
      { 

      if(i == 0) 

      //Position the first one below the cell's TextLabel 
      { 
       string time = myRecords[i].ResponseTime; 

       label.Frame = new CoreGraphics.CGRect(TextLabel.Frame.X + 10,TextLabel.Frame.Y + TextLabel.Frame.Height + 10,ContentView.Frame.Width,19); 
       label.Font = UIFont.FromName ("HelveticaNeueLTStd-ThCn", 18f); 
       label.TextColor = UIColor.DarkGray; 
       label.Text = string.Format("- {0}, {1}",otherInfo[i],time); 
       ContentView.AddSubview(label); 

       }else{ 

        string responseTime = myRecords[i].ResponseTime; 

        //Position subsequent views below the first label 
        UILabel label1 = new UILabel(); 
        label1.Frame = new CoreGraphics.CGRect(label.Frame.X,label.Frame.Bottom + 5,ContentView.Frame.Width,19); 
        label1.Font = UIFont.FromName ("HelveticaNeueLTStd-ThCn", 18f); 
        label1.TextColor = UIColor.DarkGray; 
        label1.Text = string.Format("- {0}, {1}",otherInfo[i],responseTime); 
        ContentView.AddSubview(label1); 

        } 

       } 
      } 

     }catch(Exception ex) 
     { 
      Console.WriteLine (ex.Message + ex.StackTrace); 

     } 

    } 

Antwort

0

Basierend auf der Antwort auf SO hier: How to create n number of UIButton programmatically in UITableView Cell?

konnte ich mit einer Lösung wie dieser kommen:

public void UpdateCell(List<Records> allRecords,List<string> otherInfo) 
    { 
    try{ 

     UILabel label = new UILabel(); 

     myRecords = allRecords; 

     for(int i = 0; i < myRecords.Count;i++) 
     { 

     string time = myRecords[i].ResponseTime; 

      label.Frame = new CoreGraphics.CGRect(45,TextLabel.Frame.Height + i *(labelSize + spacing),ContentView.Frame.Width,19); 
      label.Font = UIFont.FromName ("HelveticaNeueLTStd-ThCn", 18f); 
      label.TextColor = UIColor.DarkGray; 
      label.Text = string.Format("- {0}, {1}",otherInfo[i],time); 
      ContentView.AddSubview(label); 

     } 

      } 

     }catch(Exception ex) 
     { 
      Console.WriteLine (ex.Message + ex.StackTrace); 

     } 

    } 
Verwandte Themen