c# - 优化条件,否则xamarin iOS

标签 c# ios optimization xamarin

如何优化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语句也很容易阅读。

关于c# - 优化条件,否则xamarin iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25461795/

相关文章:

c# - Entity Framework 类问题中的两个相同类类型

c# - iText 7 从字节数组合并文档

c# - 在 Azure 中将机器人从 Node 更改为 C#

ios - Swift,如何将字典拆分为 60kb 的 block ?

c# - BackgroundWorker 的 RunWorkerCompleted 被触发两次

ios - 通过 api 调用在应用程序购买中添加 iTunes

android - 使用 RoboVM 将 App 上传到 Apple 的 App Store 时出现问题

javascript - 用更简洁的逻辑替换 switch 语句

python - 加快 ~50GB CSV 文件的轻处理

javascript - 在 JavaScript 中使用位移或数学创建通用数据溢出函数