0

在我的应用程序中,我只是尝试为所选行添加复选标记。我的行中有大约 20 个项目,并希望为所选行显示一个复选标记。当我滚动到页面底部并选择一行时,它会抛出 NullReferenceException。我从 这里有复选标记代码参考

using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;
using System.Collections.Generic;

namespace SampleApp
{
  public partial class FirstViewController : UITableViewController
  {
    DataSource dataSource;

    public FirstViewController () : base ("FirstViewController", null)
    {
        Title = NSBundle.MainBundle.LocalizedString ("First", "First");
        TabBarItem.Image = UIImage.FromBundle ("first");
    }

    public override void DidReceiveMemoryWarning ()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning ();

        // Release any cached data, images, etc that aren't in use.
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        // Perform any additional setup after loading the view, typically from a nib.
        // Add Table
        TableView.Source = dataSource = new DataSource (this);
    }

    public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
    {
        // Return true for supported orientations
        return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
    }

    class DataSource : UITableViewSource
    {
        static readonly NSString CellIdentifier = new NSString ("DataSourceCell");

        FirstViewController controller;

        private List<String> _listData = new List<String> ();  

        private NSIndexPath _previousRow;


        public DataSource (FirstViewController controller)
        {
            this.controller = controller;

            //  POPULATE DISPOSITION LIST
            PopulateDatabase();

            _previousRow = NSIndexPath.FromRowSection(Settings.SelectedIndex,0);

        }

        void PopulateDatabase()
        {

            _listData.Add("val1");
            _listData.Add("val2");
            _listData.Add("val3");
            _listData.Add("val4");
            _listData.Add("val5");
            _listData.Add("val6");
            _listData.Add("val7");
            _listData.Add("val8");
            _listData.Add("val9");
            _listData.Add("val10");
            _listData.Add("val11");
            _listData.Add("val12");
            _listData.Add("val13");
            _listData.Add("val14");
            _listData.Add("val15");


        }

        // Customize the number of sections in the table view.
        public override int NumberOfSections (UITableView tableView)
        {
            return 1;
        }

        public override int RowsInSection (UITableView tableview, int section)
        {
            return _listData.Count;
        }

        // Customize the appearance of table view cells.
        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell (CellIdentifier);
            if (cell == null) {
                cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier);
                //cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
            }


            cell.TextLabel.Text = _listData[indexPath.Row];
            cell.TextLabel.Font = UIFont.SystemFontOfSize(18);

            return cell;
        }


        // Row Select
        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            // Uncheck the previous row
            if (_previousRow != null)
                tableView.CellAt(_previousRow).Accessory = UITableViewCellAccessory.None;

            // Do something with the row
            var row = indexPath.Row;
            Settings.SelectedIndex = row;
            tableView.CellAt(indexPath).Accessory = UITableViewCellAccessory.Checkmark;

            //Console.WriteLine("{0} selected",_controller.Items[row]);

            _previousRow = indexPath;

            // This is what the Settings does under Settings>Mail>Show on an iPhone
            tableView.DeselectRow(indexPath,false);

        }

        // Set row height
        public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            return 50f;
        }


    }

    public class Settings
    {
        public static int SelectedIndex = 0;    
    }
}
}

我在线上遇到异常

tableView.CellAt(_previousRow).Accessory =UITableViewCellAccessory.None;

当我调试时,我发现附件

tableView.CellAt(_previousRow).Accessory    Unknown member: Accessory

知道出了什么问题吗???

得到了分析器

// Row Select
        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            // Uncheck the previous row
            if (_previousRow != null)
                       {
                               NSIndexPath [] temp = tableView.IndexPathsForVisibleRows;

               if (temp.Contains(preIndex))
               {

                 tableView.CellAt(_previousRow).Accessory = UITableViewCellAccessory.None;
                                }
                         }

            // Do something with the row
            var row = indexPath.Row;
            Settings.SelectedIndex = row;
            tableView.CellAt(indexPath).Accessory = UITableViewCellAccessory.Checkmark;

            //Console.WriteLine("{0} selected",_controller.Items[row]);

            _previousRow = indexPath;

            // This is what the Settings does under Settings>Mail>Show on an iPhone
            tableView.DeselectRow(indexPath,false);

        }
4

0 回答 0