2013-07-25 44 views
1

我有一個的tableview以下視圖控制器和一個自定義單元格:MonoTouch的打開的tableview細胞的ViewController點擊

using System; 
using System.Drawing; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using System.Linq; 
using System.Threading; 
using System.Data; 
using System.IO; 
using Mono.Data.Sqlite; 
using System.Collections.Generic; 
using Zurfers.Mobile.Core; 
using AlexTouch.MBProgressHUD; 
using System.Collections; 

namespace Zurfers.Mobile.iOS 
{ 
    public partial class iPhoneHotelSearchViewController : UIViewController 
    { 

     MBProgressHUD hud; 

     public string Destination { 
      get; 
      set; 
     } 

     public DateTime CheckInDate { 
      get; 
      set; 
     } 

     public DateTime CheckOutDate { 
      get; 
      set; 
     } 

     public int Rooms { 
      get; 
      set; 
     } 

     public iPhoneHotelSearchViewController (IntPtr handle) : base (handle) 
     { 

     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      hud = new MBProgressHUD(this.View); 
      hud.Mode = MBProgressHUDMode.Indeterminate; 
      hud.LabelText = "Loading..."; 
      hud.DetailsLabelText = "Searching Hotel"; 
      this.View.AddSubview(hud); 
      hud.Show(true); 

     } 

     public override void ViewDidAppear (bool animated) { 
      base.ViewDidAppear (animated); 
      SearchHotel(); 

     } 

     public void SearchHotel(){ 

      Hotel hotel = new Hotel(); 
      var distribution = new HotelDistribution[]{new HotelDistribution(){ Adults = 1, Children = 0, ChildrenAges = new int[0]} }; 
      var items = hotel.SearchHotels(Convert.ToDateTime("2013-08-08"),Convert.ToDateTime("2013-09-09 "),"(MIA)", distribution,"","","",0); 


      List<DtoHotelinformation> data = new List<DtoHotelinformation>(); 

      foreach (var item in items) 
      { 
       DtoHotelinformation DtoHotelinformation = new DtoHotelinformation(); 
       DtoHotelinformation.code = item.Code.ToString(); 
       DtoHotelinformation.price = item.Price.ToString(); 
       DtoHotelinformation.title = item.Name.ToString().ToTitleCase(); 
       DtoHotelinformation.subtitle = item.Address.ToString(); 
       DtoHotelinformation.rating = item.Rating.ToString(); 
       DtoHotelinformation.imageUlr = item.ImageUrl; 

       data.Add(DtoHotelinformation); 
      } 

      hud.Hide(true); 
      hud.RemoveFromSuperview(); 
      HotelSearchTable.Source = new HotelTableSource(data.ToArray()); 
      HotelSearchTable.ReloadData(); 

     } 

     partial void GoBack (MonoTouch.Foundation.NSObject sender) 
     { 
      DismissViewController(true, null); 
     } 
    } 
} 

現在表源

using System; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 

namespace Zurfers.Mobile.iOS 
{ 
    public class HotelTableSource : UITableViewSource 
    { 

     DtoHotelinformation[] tableItems; 
     NSString cellIdentifier = new NSString("TableCell"); 



     public HotelTableSource (DtoHotelinformation[] items) 
     { 
      tableItems = items; 
     } 
     public override int RowsInSection (UITableView tableview, int section) 
     { 
      return tableItems.Length; 
     } 

     public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
     { 
      //WHAT TO DO HERE 

      tableView.DeselectRow (indexPath, true); // normal iOS behaviour is to remove the blue highlight 
     } 

     public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) 
     { 
      CustomCell cell = tableView.DequeueReusableCell(cellIdentifier) as CustomCell; 

      if (cell == null) 
       cell = new CustomCell(cellIdentifier); 
      cell.UpdateCell(tableItems[indexPath.Row].title, tableItems[indexPath.Row].subtitle, tableItems[indexPath.Row].price, 
          tableItems[indexPath.Row].imageUlr, tableItems[indexPath.Row].rating); 
      return cell; 

     } 

     public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath) 
     { 
      return 70; 
     } 
    } 
} 

最後customcell代碼:

using System; 
using System.Drawing; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using MonoTouch.Dialog.Utilities; 

namespace Zurfers.Mobile.iOS 
{ 
    public class CustomCell : UITableViewCell, IImageUpdated 
    { 
     UILabel headingLabel, subheadingLabel, priceLabel; 
     UIImageView imageService; 
     UIImageView star, star2, star3, star4, star5; 
     public CustomCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId) 
     { 
      imageService = new UIImageView(); 
      star = new UIImageView(); 
      star2 = new UIImageView(); 
      star3 = new UIImageView(); 
      star4 = new UIImageView(); 
      star5 = new UIImageView(); 
      headingLabel = new UILabel(){ 
       Font = UIFont.FromName("Verdana-Bold", 14f), 
       BackgroundColor = UIColor.Clear, 
       TextColor = UIColor.FromRGB(241, 241, 211) 
      }; 
      subheadingLabel = new UILabel(){ 
       Font = UIFont.FromName("Verdana-Bold", 8f), 
       TextColor = UIColor.FromRGB(255, 255, 255), 
       BackgroundColor = UIColor.Clear 
      }; 
      priceLabel = new UILabel(){ 
       Font = UIFont.FromName("Verdana", 14f), 
       TextColor = UIColor.FromRGB(241, 241, 211), 
       BackgroundColor = UIColor.Clear 
      }; 

      AddSubview(imageService); 
      AddSubview(headingLabel); 
      AddSubview(subheadingLabel); 
      AddSubview(priceLabel); 
      AddSubview(star); 
      AddSubview(star2); 
      AddSubview(star3); 
      AddSubview(star4); 
      AddSubview(star5); 

     } 

     public void UpdateCell (string title, string subtitle, string price, string imageUlr, string rating) 
     { 
      if (imageUlr != null) { 
       var u = new Uri(imageUlr); 
       ImageLoader MyLoader= new ImageLoader(50,50); 
       imageService.Image = MyLoader.RequestImage(u,this); 

      } else { 
       imageService.Image = UIImage.FromFile("generic_no_image_tiny.jpg"); 
      } 


      headingLabel.Text = title; 
      subheadingLabel.Text = subtitle; 
      if (subtitle.Length > 40) { 
       subheadingLabel.LineBreakMode = UILineBreakMode.WordWrap; 
       subheadingLabel.Lines = 0; 
      } 

      switch (rating) { 
       case "T": 
        star.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star2.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        break; 
       case "S": 
        star.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star2.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star3.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        break; 
       case "F": 
        star.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star2.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star3.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star4.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        break; 
       case "L": 
        star.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star2.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star3.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star4.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        star5.Image = UIImage.FromFile("ZurfersMovil-Stars.png"); 
        break; 
      } 


      priceLabel.Text = "USD " + price; 
      priceLabel.Font = UIFont.BoldSystemFontOfSize (16); 
     } 


     public void UpdatedImage (Uri uri) 
     { 
      imageService.Image = ImageLoader.DefaultRequestImage(uri, this); 
     } 


     public override void LayoutSubviews() 
     { 
      base.LayoutSubviews(); 
      imageService.Frame = new RectangleF(10, 10, 50, 33); 
      headingLabel.Frame = new RectangleF(70, 4, 240, 25); 
      subheadingLabel.Frame = new RectangleF(70, 25, 240, 20); 
      priceLabel.Frame = new RectangleF(220, 45, 100, 20); 
      star.Frame = new RectangleF(70, 45, 15, 15); 
      star2.Frame = new RectangleF(85, 45, 15, 15); 
      star3.Frame = new RectangleF(100, 45, 15, 15); 
      star4.Frame = new RectangleF(115, 45, 15, 15); 
      star5.Frame = new RectangleF(130, 45, 15, 15); 
     } 
    } 
} 

我想打開另一個viewcontroller(iPhoneHotelDetailViewController)當用戶觸摸表格視圖的單元格。但我不知道如何做到這一點。

請幫我。

在此先感謝您的幫助。

回答

3

通常,您希望NavigationController成爲您的應用中的「頂級」元素,包裝所有其他控制器。

在您的AppDelegate中,創建一個NavigationController,並將其作爲您的應用程序的根目錄。

然後創建您的搜索控制器的一個實例,並將其推送到NavigationController。

最後,將NavigationController屬性添加到TableSource的構造函數中。

NavigationController nav; 

public HotelTableSource (DtoHotelinformation[] items, NavigationController nav) 
{ 
    this.nav = nav; 
    tableItems = items; 
} 

當您創建TableSource,通過在NavigationController參考,你可以做到這一點,因爲所有ViewControllers有,如果他們都包含一個內指向他們NavigationController,屬性。

HotelSearchTable.Source = new HotelTableSource(data.ToArray(), this.NavigationController); 

最後,在RowSelected,創建要顯示新的視圖控制器的實例:

public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
    { 
     //WHAT TO DO HERE 
     MyDetailController myDetail = new MyDetailController(); 
     nav.PushViewController(myDetail, true); 

     tableView.DeselectRow (indexPath, true); // normal iOS behaviour is to remove the blue highlight 
    } 
+0

我可以顯示另一個viewcontroller沒有navigationcontroller? – Jean

+0

是的,每個ViewController都有一個PresentViewController和PresentModalViewController方法,您也可以在沒有導航控制器的情況下使用 – Jason

+0

Jason,這不起作用。我沒有out參數的構造函數,如果我添加一個帶有out參數的構造函數,我得到一個System.NullReferencesException異常,我也沒有導航控制器。這是我的代碼: UINavigationController controller = new UINavigationController(); \t \t \t iPhoneHotelDetailViewController myDetail = new iPhoneHotelDetailViewController(); \t \t \t controller.NavigationController.PushViewController(myDetail,true); \t \t \t tableView.DeselectRow(indexPath,true); – Jean

2

我認爲UITableViewSource鏈接到UINavigationController(UI組件)是有點怪。我建議使用基於事件的方法:

  • 申報事件UITableViewSource並調用它的行選擇:
public event Action<int> OnRowSelect; 
... 
public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
{ 
    tableView.DeselectRow (indexPath, true); // normal iOS behaviour is to remove the blue highlight 

    if (OnRowSelect != null) { 
     OnRowSelect(indexPath.Row); 
    } 
} 
  • 然後,在UIViewController處理事件 - 推新UIViewController
var source = data.ToArray(); 
source.OnRowSelect += HandleOnRowSelect; 
HotelSearchTable.Source = new HotelTableSource(); 
HotelSearchTable.ReloadData(); 
... 
void HandleOnRowSelect(int index) 
{ 
    var data = items[index]; 

    // Pass data to new view controller and push it 
} 

提示到避免內存泄漏:當Pop這個UIViewController或製作新的UITableViewSource實例時,不要忘記取消訂閱OnRowSelect。一世。e:

  • source聲明爲類成員;從它
  • 退訂的事件,例如,ViewWillDisappear
source.OnRowSelect -= HandleOnRowSelect; 
0

如果您正在使用StoryBord有一個很簡單的方法來做到這一點。然後,您會像這樣在PrepareForSegue方法中傳遞數據。

public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender) 
     { 
      base.PrepareForSegue (segue, sender); 

      NSIndexPath indexPatch = tableView.IndexPathForSelectedRow; 

      if (segue.Identifier.Equals ("showHotelDetail")) { 

        var vc = segue.DestinationViewController as iPhoneHotelDetailViewController; 
        if (vc != null) { 

         //Pass some date to the iPhoneHotelDetailViewController if needed. 
         vc.hotelName = this.tableItems [indexPatch.Row].hotelName; 

        } 
      } 
     } 

在你的故事板與iPhoneHotelDetailViewController連接customCell並調用SEGUE「showHotelDetail」爲例。