2011-12-04 18 views
0

考慮下面的代碼:如何定位的UITableView(MonoTouch的)

public class MainTableController : UITableViewController 
{ 
    // Allow us to set the style of the TableView 
    public MainTableController(UITableViewStyle style) : base(style) 
    { 
    } 

    public override void ViewDidLoad() 
    { 
     TableView.DataSource = new MainTableDataSource(); 
     TableView.Delegate = new MainTableDelegate(this); 

     TableView.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile ("Content/[email protected]")); 

     TableView.Bounces = false; 

     base.ViewDidLoad(); 
    } 
} 

鑑於以下的輸出:

enter image description here

我如何更新我的代碼,以便組表格單元格開始的在頁面的一半?實際的像素並不重要,我只是想弄清楚屬性要修改的地方。

回答

3

將UITableView手動定位到UITableViewController中並不是一個好主意。話雖這麼說,我認爲調整ContentInset屬性應該把你帶到任何你需要去:

this.TableView.ContentInset = new UIEdgeInsets(this.TableView.Center.Y, 0, 0, 0); 

這應該推下第1節/電池到指定UIEdgeInset.Top值。

編輯:按澄清評論...

如果你需要保持背景視圖的位置......你可能會通過創建第一部分中的HeaderView並增加有更好的運氣它的框架到足以將第一個單元向下移動的尺寸:

public override UIView GetViewForHeader (UITableView tableView, int sectionIdx) 
{ 
    if(sectionIdx == 0) { 

     // adjust appropriate index above^

     var view = new UIView(); 
     view.Frame = new RectangleF(...) // adjust your frame here 
     return view;  
    } 
} 
+0

手動定位有什麼危害?我是新手,如果您能提出更好的解決方案來滿足我的需求,請讓我知道,因爲我想遵守最佳做法。感謝你的回答。 –

+0

通常,UIViewController的視圖(UITableViewController只是從UIViewController繼承而來)* always *想要全屏,除非它在iOS 5.x中使用自定義容器API。底線:他們已經證明,如果手動定位,你會得到不可靠的行爲,並且他們已經規定了更好的方法。上面的代碼應該會給你想要的效果。 – Anuj

+0

謝謝。這可能是一個不同的問題,但如果你注意到我上面的代碼,我有一個使用的背景圖像。當我實施您的解決方案時,它也將向下推送背景圖像。這個問題最初的原因是因爲實際上UITableView背後有一個需要可見的標識。你的解決方案將UITableView向下推,但也與圖像一樣。 –