2014-09-02 141 views
0

UITableView只顯示數組的第二個值...我的錯誤在哪裏?使用數組填充UITableView

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) { 

UITableViewCell cell = tableView.DequeueReusableCell(cellID);

 if (cell == null) { 
      cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellID); 
     } 

string firstValue = "Hello" 
string secondValue = "Bye" 

string[] concat = {firstValue, secondValue}; 

     foreach(string op in concat){ 
       cell.TextView.Text = op; 
     } 

return cell;} 

回答

2

您正在對同一個變量進行多個賦值,因此最後一個賦值將覆蓋之前的賦值。爲了追加文本可以使用+=操作

foreach(string op in concat){ 
     cell.TextView.Text += op; 
} 
+0

這完全回答這個問題;然而,取決於concat中實際有多少項目,它可能對StringBuilder有更好的性能.Append() – valdetero 2014-09-02 18:23:48

0

foreach語句將遍歷數組中的每個字符串和細胞的的TextView的text屬性設置爲當前循環字符串。

嘗試:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) { 
UITableViewCell cell = tableView.DequeueReusableCell (cellID); 

if (cell == null) { 
    cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellID); 
} 

string firstValue = "Hello" 
string secondValue = "Bye" 

string[] concat = {firstValue, secondValue}; 

foreach(string op in concat){ 
    cell.TextView.Text += op; 
} 

return cell; 

} 

這將串聯數組您單元的TextView的文本中的每個字符串。所以它會導致「你好再見」。


編輯:如果你想使用每個數組值的一個新行:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) { 
    UITableViewCell cell = tableView.DequeueReusableCell (cellID); 



if (cell == null) { 
    cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellID); 
} 

string firstValue = "Hello" 
string secondValue = "Bye" 

string[] concat = {firstValue, secondValue}; 

cell.TextView.Text = concat[indexPath!.row]; 

return cell; 

} 
+0

你說得對,它有一點幫助,但我不想在同一行進行連接...這兩個字符串寫在一起的方式。我要完成的表firstValue連續和secondValue另一行 – 2014-09-02 18:32:38

+0

@RomuloViel我的答案更新。 – Sebyddd 2014-09-02 18:36:15

+0

我沒有如何使用indexPath!.row。 – 2014-09-02 18:43:49