2016-08-02 1 views
0

解決方法はかなりシンプルかもしれませんが、これらのビューを配置する際に問題があります。Monotouch - UILabelsを相互に下にプログラムで作成するためにforループを使用する

私はUITableViewCellの範囲内でUILabelsを動的に作成しようとしています。コンパイル時にはUILabelsの数は不明です。

私のデータリストのサイズに基づいて、新しいUILabelを作成し、リストの各インデックスのデータをそれぞれUILabelに入力したいとします。

これらはUITableViewCellの内部に作成されているため、セルのTextLabelの下に配置してください。

現在のところ、私の新しいUILabelsはすべて、お互いの上に重なっており、TextLabelの上に積まれており、正しく配置されていないようです。だからここでの回答に基づいて

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); 

     } 

    } 

答えて

0

How to create n number of UIButton programmatically in UITableView Cell?

私はこのような解決策を考え出すことができました:

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); 

     } 

    } 
関連する問題