Как оптимизировать if else условие для цвета ячейки tableview
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath){
var cell = tableView.DequeueReusableCell (TableCell.Key) as TableCell;
if (cell == null)
cell = new TableCell ();
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
if (indexPath.Row % 2 == 0) {
cell.BackgroundColor = UIColor.White;
} else {
cell.BackgroundColor = UIColor.LightGray;
}}
Здесь мало что осталось для оптимизации. Единственное, что я бы изменил, это последнее, if
бы я заменил его условным выражением:
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath){
var cell = tableView.DequeueReusableCell (TableCell.Key) as TableCell;
if (cell == null) {
cell = new TableCell ();
}
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
cell.BackgroundColor = (indexPath.Row % 2 == 0) ? UIColor.White : UIColor.LightGray;
}
Это вопрос личного предпочтения, хотя: ваш оператор if
с двумя присваиваниями был вполне читаемым.
Вы также можете использовать? для создания ячейки:
var cell = tableView.DequeueReusableCell (TableCell.Key) as TableCell ?? new TableCell();
По внешнему виду это также может быть небольшим улучшением, поскольку аксессуар кажется статическим типом и не нужно назначать каждый раз:
var cell = tableView.DequeueReusableCell (TableCell.Key) as TableCell
?? new TableCell() { Accessory = UITableViewCellAccessory.DisclosureIndicator };