user10917327
user10917327

Reputation:

Populate UITableView with array

The UITableView just show the second value of array... Where's my error?

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

Upvotes: 0

Views: 451

Answers (2)

Sebyddd
Sebyddd

Reputation: 4315

The foreach statement will loop through each string in your array and set the cell's textView's text property to the currently looped string.

Try:

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;

}

This will concatenate each string in the array to your cell's textView's text. So it will result in "Hello Bye".


EDIT: If you want use each of your array value on a new row:

 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;

}

Upvotes: 0

Jason
Jason

Reputation: 89082

You are making multiple assignments to the same variable, so the last assignment will overwrite any previous assignments. To append text you can use the += operator

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

Upvotes: 2

Related Questions