Question

I have a series of StyledStringElements in a Monotouch.Dialog UITableView.

I wish to set every other row to have a different background color (financial report).

Is there an existing way to do this and if not, what can I override to enable this?

In a standard UITableView I would create a new delegate for the table like this:

public class AlternatingTableViewDelegate : UITableViewDelegate
{
    public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
    {
        if (indexPath.Row%2 == 0)
        {
            cell.BackgroundColor = UIColor.White;
        }
        else
        {
            cell.BackgroundColor = Settings.ColorAlternatingRow;
        }
    }
}
Was it helpful?

Solution

I solved this by adding the below helper into my BaseDialogViewController class which subclasses DialogViewController

    internal int RowCount = 0;

    protected StyledStringElement Alternate(StyledStringElement element)
    {
        if (++RowCount % 2 == 0) element.BackgroundColor = Settings.ColorAlternatingRow;
        return element;
    }

Then in code after I create an element, I pass it to this function to style up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top