Mono Touch Table应用

using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;

namespace BasicTable
{
    public class Application 
    {
        public static void Main (string[] args)
        {
            try 
            {
                UIApplication.Main (args, null, "AppDelegate");
            } 
            catch (Exception e) 
            {
                Console.WriteLine (e.ToString ());
            }
        }
    }

    [Register("AppDelegate")]
    public class AppDelegate : UIApplicationDelegate
    {
        protected UIWindow window;
        protected HomeScreen iPhoneHome;
        
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            window.MakeKeyAndVisible ();
                        
            iPhoneHome = new HomeScreen();
            iPhoneHome.View.Frame = new System.Drawing.RectangleF(0
                        , UIApplication.SharedApplication.StatusBarFrame.Height
                        , UIScreen.MainScreen.ApplicationFrame.Width
                        , UIScreen.MainScreen.ApplicationFrame.Height);
            
            window.AddSubview (iPhoneHome.View);
            
            return true;
        }
    }
}


using System;
using System.Drawing;
using System.Collections.Generic;
using MonoTouch.UIKit;

namespace BasicTable
{
    public class HomeScreen : UIViewController 
    {
        public HomeScreen ()
        {
        }

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

            UITableView table = new UITableView (View.Bounds); //defaults to Plain style
            table.AutoresizingMask = UIViewAutoresizing.All;
            string[] tableItems = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
            table.Source = new TableSource (tableItems);
            this.View.Add (table);
        }
    }
}


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

namespace BasicTable 
{
    public class TableSource : UITableViewSource 
    {
        protected string[] tableItems;
        protected string cellIdentifier = "TableCell";
    
        public TableSource (string[] items)
        {
            tableItems = items;
        }
    
        /// <summary>
        /// Called by the TableView to determine how many cells to create for that particular section.
        /// </summary>
        public override int RowsInSection (UITableView tableview, int section)
        {
            return tableItems.Length;
        }
        
        /// <summary>
        /// Called when a row is touched
        /// </summary>
        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            new UIAlertView ("Row Selected",
                             tableItems [indexPath.Row], null, "OK", null).Show ();
            tableView.DeselectRow (indexPath, true);

//            bool ch = tableView.VisibleCells [indexPath.Row].Accessory == UITableViewCellAccessory.Checkmark;
//            if (ch) {            
//                tableView.VisibleCells [indexPath.Row].Accessory = UITableViewCellAccessory.DisclosureIndicator;
//            } else {
//                tableView.VisibleCells [indexPath.Row].Accessory = UITableViewCellAccessory.Checkmark;
//            }
        }

//        public override void AccessoryButtonTapped (UITableView tableView, NSIndexPath indexPath)
//        {
//            new UIAlertView ("DetailDisclosureButton Touched"
//                            , tableItems [indexPath.Row], null, "OK", null).Show ();
//        }

        /// <summary>
        /// Called by the TableView to get the actual UITableViewCell to render for the particular row
        /// </summary>
        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            // request a recycled cell to save memory
            UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
            // if there are no cells to reuse, create a new one
            if (cell == null)
                cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);

            if (indexPath.Row == 0) {
                cell.ImageView.Image = UIImage.FromFile ("Images/Icons/50_icon.png");
            }
            cell.TextLabel.Text = tableItems [indexPath.Row];
            cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
            cell.DetailTextLabel.Text = "kkkk";
            
            return cell;
        }

        //...
    }
}

效果图如下:




原文地址:https://www.cnblogs.com/keanuyaoo/p/3424043.html