Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在UITableView中插入新行的技术_Objective C_Swift_Uitableview_Xamarin_Xamarin.ios - Fatal编程技术网

Objective c 在UITableView中插入新行的技术

Objective c 在UITableView中插入新行的技术,objective-c,swift,uitableview,xamarin,xamarin.ios,Objective C,Swift,Uitableview,Xamarin,Xamarin.ios,目前,我正在尝试在iOS中创建一个嵌套列表视图,该表类似于p列表,这意味着每次用户点击每个单元格开头的加号时,它下面都会出现新行 目前,我认为解决方案是:单击按钮时,更新表的数据源,然后在单击的索引下插入新行(这就是我卡住的地方!) 原始数据源是一个字符串数组:{“a”、“b”、“c”、“d”} 在每条记录之后添加的数据是:{“sub-1”、“sub-2”、“sub-3”} 我正在用C#编写这个演示,但我能够理解swift和obj-C,所以请随意发布您喜欢的任何语言 我的代码目前包括3个主要类:

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

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

原始数据源是一个字符串数组:{“a”、“b”、“c”、“d”}

在每条记录之后添加的数据是:{“sub-1”、“sub-2”、“sub-3”}

我正在用C#编写这个演示,但我能够理解swift和obj-C,所以请随意发布您喜欢的任何语言

我的代码目前包括3个主要类:
CustomtableSource.cs
CustomTableViewCell.cs
ExpandableListViewController.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;
    }        
}
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();
        };*/
    }
}
公共类ExpandableListViewController:UIViewController
{
私有UITableView _tableView;
专用UIButton rowButton;
public ExpandableListViewController():base(“ExpandableListViewController”,null)
{                        
}
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
变量宽度=View.Bounds.width;
var height=View.Bounds.height;
_tableView=新的UITableView(新的CGRect(0,0,宽度,高度));
//_rowButton=CustomTableViewCell.\u按钮;
_tableView.AutoresizingMask=ui视图autoresizing.All;
列表书籍=新列表();
添加(newbook(){BookTitle=“C编程”,Level=0});
books.Add(newbook(){BookTitle=“初学者”,Level=1});
books.Add(newbook(){BookTitle=“mediate”,Level=1});
books.Add(newbook(){BookTitle=“Advanced”,Level=1});
添加(newbook(){BookTitle=“Java编程”,Level=2});
books.Add(newbook(){BookTitle=“Basic”,Level=1});
添加(newbook(){BookTitle=“javaswing”,Level=1});
添加(newbook(){BookTitle=“Pascal编程”,Level=0});
//添加(newbook(){BookTitle=“CoBol编程”,Level=0});
CustomTableSource tableSource=新的CustomTableSource(图书);
_tableView.Source=tableSource;
添加(_tableView);
/*_rowButton.TouchUpInside+=(发送方,参数)=>
{
_rowButton.SetImage(UIImage.FromFile(“Images/open.png”)、UIControlState.Normal);
_tableView.beginUpdate();
添加(newbook(){BookTitle=“CoBol编程”,Level=0});
_tableView.ReloadData();
_EndUpdates();
};*/
}
}
我所做工作的屏幕截图:


将这些代码添加到
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);
}

这里有一篇关于这个的博客:谢谢你的建议。有更接近我问题的例子吗?
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);
}
tableSource. OnRowItemSelected += (s, e) => {
    //Insert new sub item list
    books.Insert(e, new Book { /*init*/ } );

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