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