UIView单点触控内的UITableView

本文关键字:UITableView 单点触 UIView | 更新日期: 2024-09-24 08:49:26

我正在做一个简单的应用程序,它包含一个带有自定义单元格的UITableView,我已经阅读了本教程http://www.arcticmill.com/2012/05/uitableview-with-custom-uitableviewcell.html一切都很有魅力,但我不知道如何在UIView或UIScrollView中添加UITableView,这样表就不会占用所有屏幕。

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;
}
 }
}

正如我所看到的,ListSource继承了UITableViewSource,但我真的不知道如何将其添加到另一个ScrollView 中

UIView单点触控内的UITableView

您可以像使用UIViewController或UIView一样使用滚动视图。

在UIViewController中,你会说

[self.view addSubview:tableView];

为了使用滚动视图做到这一点,您只需创建一个滚动视图,然后创建一个特殊表视图的实例并将其添加到滚动视图的视图中,然后将滚动视图添加到UIViewController的视图中。像这样:

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    UITableView *tableView = [[UITableView alloc] initWithFrame:scrollView.frame style:UITableViewStylePlain];
    [scrollView addSubview:tableView];
    [self.view addSubview:scrollView];

您可以对滚动视图的边框大小和其他属性执行任何操作。

在C#中,你会做:

this.View.AddSubview (tableView);

和:

    var scrollView = new UIScrollView (view.Frame);
    var tableView =  new UITableView (scrollView.Frame, UITableViewStyle.Plain);
    scrollView.AddSubview (tableView);
    View.AddSubview (scrollView);