iPhone编程——用类型对象填充表

本文关键字:填充 对象 编程 iPhone 类型 | 更新日期: 2023-09-27 18:14:59

我试图创建一个表格,填充的是文本字段和标签,但我有一个棘手的时间填充它。如果表只接受字符串作为单元格,就会抛出错误。

我的代码如下——我试图使它尽可能简短。

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

namespace Hello_Multiscreen
{
public class TableSource : UITableViewSource {
    object[] tableItems;
    string cellIdentifier = "TableCell";
    public TableSource (object[] items)
    {
        tableItems = items;
    }
    public override int RowsInSection (UITableView tableview, int section)
    {
        return tableItems.Length;
    }
    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
        // if there are no cells to reuse, create a new one
        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
        return cell;
    }
}
}

在使用这个表的screen类中,ViewDidLoad方法有

public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            // adds the table to the view
            table = new UITableView(View.Bounds); // defaults to Plain style
        // load in a string of the values from that database corresponds to all scope--allow user to click on them
UITextField txtFld = new UITextField();
        object[] tableItems = new object[] {txtFld, txtFld, txtFld};
        // select a table source--the table name is the same as the control id name that was dragged
        table.Source = new TableSource(tableItems);
        Add (table);
    }

iPhone编程——用类型对象填充表

你需要创建一个自定义的UITableViewCell。标准的TableCell有文本标签,但没有任意对象或输入字段。

Xamarin提供MonoTouch。对话框,用于构建表,并包含不同类型输入字段的多个选项。