在UITableView中插入新行的技巧

时间:2016-02-19 03:37:40

标签: objective-c swift uitableview xamarin xamarin.ios

目前我尝试在iOS中创建嵌套列表视图,此表类似于p列表,这意味着每次用户点击每个单元格开头的加号时,都会出现新行低于它。

目前,我认为解决方案是:点击按钮时,更新表格的数据源,然后在点击的索引下面插入新行(这就是我卡住的地方!)。

原始数据源是一个字符串数组:{" a"," b"," c"," d"}

每条记录后添加的数据为:{" sub-1"," sub-2"," sub-3"}

我在C#中编写此演示,但我能够理解swift和obj-c,所以随时发布您喜欢的任何语言!

我的代码目前包含3个主要类:CustomtableSource.cs CustomTableViewCell.csExpandableListViewController.cs

CustomTableSource.cs:

class CustomTableSource : UITableViewSource
{
    private List<Book> _books;
    string CellIdentifier = "TableCell";            

    public CustomTableSource(List<Book> books)
    {
        _books = books;            
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var customCell = tableView.DequeueReusableCell(CellIdentifier) as CustomTableViewCell;            
        Book book = _books[indexPath.Row];
        UIImage img = UIImage.FromFile("Images/closed.png");

        if (customCell == null)
        {
            customCell = new CustomTableViewCell(CellIdentifier);
            customCell.UpdateCell(book.BookTitle, img);
        }

        return customCell;
    }

    public override nint RowsInSection(UITableView tableview, nint rowInSection)
    {
        return _books.Count;
    }

    public override nint NumberOfSections(UITableView tableView)
    {
        return 1;
    }        
}

CustomTableViewCell.cs

public class CustomTableViewCell : UITableViewCell
{
    private UIButton _button;// = UIButton.FromType(UIButtonType.Custom);
    private UILabel _title;

    public CustomTableViewCell(string cellId) : base (UITableViewCellStyle.Default, cellId)
    {
        SelectionStyle = UITableViewCellSelectionStyle.Gray;
        ContentView.BackgroundColor = UIColor.Clear;

        _button = UIButton.FromType(UIButtonType.Custom);
        /* _button.TouchUpInside += (sender, args) =>
        {
            _button.SetImage(UIImage.FromFile("Images/open.png"), UIControlState.Normal);

        };*/
        _title = new UILabel();
        _title.BackgroundColor = UIColor.Clear;

        ContentView.AddSubviews(new UIView[] {_button, _title});
    }

    public void UpdateCell(string title, UIImage image)
    {
        _button.SetImage(image, UIControlState.Normal);
        _title.Text = title;
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        _button.Frame = new CGRect(12, 22, 10, 10);
        _title.Frame = new CGRect(30, 5, ContentView.Bounds.Width, ContentView.Bounds.Height);
    }
}

ExpandableListViewController.cs:

public class ExpandableListViewController : UIViewController
{
    private UITableView _tableView;
    private UIButton _rowButton;

    public ExpandableListViewController() : base("ExpandableListViewController", null)
    {                        
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        var width = View.Bounds.Width;
        var height = View.Bounds.Height;
        _tableView = new UITableView(new CGRect(0, 0, width, height));
        //_rowButton = CustomTableViewCell._button;            

        _tableView.AutoresizingMask = UIViewAutoresizing.All;

        List<Book> books = new List<Book>();
        books.Add(new Book() { BookTitle = "C programming", Level = 0 });
        books.Add(new Book() { BookTitle = "Beginner", Level = 1 });
        books.Add(new Book() { BookTitle = "Moderate", Level = 1 });
        books.Add(new Book() { BookTitle = "Advanced", Level = 1 });
        books.Add(new Book() { BookTitle = "Java programming", Level = 2 });
        books.Add(new Book() { BookTitle = "Basic", Level = 1 });
        books.Add(new Book() { BookTitle = "Java Swing", Level = 1 });
        books.Add(new Book() { BookTitle = "Pascal programming", Level = 0 });
        //books.Add(new Book() { BookTitle = "CoBol programming", Level = 0 });

        CustomTableSource tableSource = new CustomTableSource(books);

        _tableView.Source = tableSource;
        Add(_tableView);

        /*_rowButton.TouchUpInside += (sender, args) =>
        {
            _rowButton.SetImage(UIImage.FromFile("Images/open.png"), UIControlState.Normal);
            _tableView.BeginUpdates();
            books.Add(new Book() { BookTitle = "CoBol programming", Level = 0 });
            _tableView.ReloadData();                
            _tableView.EndUpdates();
        };*/
    }
}

我所做的截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

将这些代码添加到CustomTableSource.cs以在用户选择行时引发事件。

public event EventHandler<int> OnRowItemSelected;

public override void RowSelected(UITableView tableView, Foundation.NSIndexPath indexPath)
{
    if (OnRowItemSelected != null)
    {
        OnRowItemSelected(tableView, indexPath.Row);
    }
    tableView.DeselectRow(indexPath, true);
}

ExpandableListViewController.cs,订阅行选择的事件,在引发时添加新书并重新加载表

tableSource. OnRowItemSelected += (s, e) => {
    //Insert new sub item list
    books.Insert(e, new Book { /*init*/ } );

    //reload table
    _tableView.ReloadData();
};