简体   繁体   中英

UITableView Multiple Columns Implementation

I, like I see many others am struggling to come to terms with the fact that UITableView does not support multiple columns. I've seen people mention that UICollectionView "does this", but with the limited documentation/examples available I don't understand how this is best implemented.

I've just come from an Android build where this was simple, however I've wasted several hours trying to come to grips with a solution to this trivial issue on iOS.

I'm using Xamarin, and all I am trying to do is display a shopping cart.

4 columns: name, qty, price, and a delete button.

I've been trying to implement 4 separate UITableView's and I have been playing with the sample UITableViewSource implementation found here: https://developer.xamarin.com/recipes/ios/content_controls/tables/populate_a_table/ . This however, relies on a string array for the values and just feels like a hack. I'm not sure how I would modify this so that I could pass in a button which had its own click event.

Could someone please help explain how I would best go about getting around the trivial task of displaying a shopping cart?

I'm also unsure how best to set the headings "Name", "Qty", "Price" etc. Do I just add them as another row?

Any help is greatly appreciated.

You should use custom cell with any layout you want.

Just create Cell( in Xamarin Studio right click on folder in Solution Explorer -> Add -> New File -> iOS -> iPhone TableView Cell) and layout it what ever you want.

Then override method in table source:

public class CustomTableSource : UITableSource
{
    IEnumerable datasource;
    const string cellIdentifier = "YourCustomCell";  

    public CustomTableSource(IEnumerable datasource)
    {
        this.datasource = datasource;
    }  

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        return datasource.Count();
    }   

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell (cellIdentifier) as YourCustomCell;
        if (cell == null)
            cell = new YourCustomCell(cellIdentifier);
        // populate cell here
        // for if your cell has UpdateData method 
        cell.UpdateData(datasource[indexPath.Row]);

        return cell;
    }
}

More info can be find here

UPD TableSource Sample

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM