简体   繁体   中英

UITableView inside UIView Monotouch

I'm doing a simple app that involves a UITableView with custom Cells, I've read this tutorial http://www.arcticmill.com/2012/05/uitableview-with-custom-uitableviewcell.html Everything works like a charm, but I don't know how to add the UITableView inside a UIView or an UIScrollView so the table doesn't use all the screen.

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

namespace CustomUITableViewCellSample
{
public class ListSource : UITableViewSource
{
 private List<string> _testData = new List<string> ();

 public ListSource ()
{
  _testData.Add ("Green");
_testData.Add ("Red");
_testData.Add ("Blue");
_testData.Add ("Yellow");
_testData.Add ("Purple");
_testData.Add ("Orange");   
}

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath    indexPath)
{
 // Reuse a cell if one exists
 CustomListCell cell = tableView.DequeueReusableCell ("ColorCell") as CustomListCell;

if (cell == null) {   
 // We have to allocate a cell
 var views = NSBundle.MainBundle.LoadNib ("CustomListCell", tableView, null);
 cell = Runtime.GetNSObject (views.ValueAt (0)) as CustomListCell;
}

  // This cell has been used before, so we need to update it's data
cell.UpdateWithData (_testData [indexPath.Row]);   

return cell;
  }

 public override int RowsInSection (UITableView tableview, int section)
 {
   return _testData.Count;
}
 }
}

As I can see, ListSource inherits from UITableViewSource but I really don't have a clue on how to add it into another ScrollView

You would at it to a scroll view just like you would to a UIViewController or UIView.

In a UIViewController you would say

[self.view addSubview:tableView];

In order to do that with a scroll view you just create a scroll view, then create an instance of your special table view and add it to the scroll view's view, then add the scroll view to the UIViewController's View. Like so:

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];

    UITableView *tableView = [[UITableView alloc] initWithFrame:scrollView.frame style:UITableViewStylePlain];

    [scrollView addSubview:tableView];

    [self.view addSubview:scrollView];

You can do whatever you want with the scroll view's frame size and other properties.

In C# you would do:

this.View.AddSubview (tableView);

And:

    var scrollView = new UIScrollView (view.Frame);

    var tableView =  new UITableView (scrollView.Frame, UITableViewStyle.Plain);

    scrollView.AddSubview (tableView);

    View.AddSubview (scrollView);

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