【问题标题】:UITableView inside UIView MonotouchUIView Monotouch里面的UITableView
【发布时间】:2013-12-16 01:14:49
【问题描述】:

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

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 中

【问题讨论】:

    标签: c# ios uitableview xamarin.ios


    【解决方案1】:

    您可以像使用 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多