2017-02-02 26 views
0

我创建了一个的UITableViewCell但设置的UITableView的来源时,我得到一个的NullReferenceExceptionXamarin的iOS UITableView.Source的NullReferenceException

public partial class SessionOverviewViewController : UIViewController 
{ 

    public List<BaseSessionController> Sessions; 

    public SessionOverviewViewController (IntPtr handle) : base (handle) 
    { 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     tvTable.RegisterClassForCellReuse(typeof(SessionTableViewCell), "SessionCell"); 
     SessionTableViewSource source = new SessionTableViewSource(Sessions); 
     tvTable.Source = source; //This line throws the Exception 
    } 
} 

的TableViewSource:

public class SessionTableViewSource : UITableViewSource 
{ 
    List<BaseSessionController> TableItems; 
    string CellIdentifier = "SessionCell"; 

    public SessionTableViewSource(List<BaseSessionController> items) 
    { 
     TableItems = items; 
    } 

    public override nint RowsInSection(UITableView tableview, nint section) 
    { 
     return TableItems.Count; 
    } 

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
    { 
     SessionTableViewCell cell = (SessionTableViewCell)tableView.DequeueReusableCell(CellIdentifier); 
     BaseSessionController friend = TableItems[indexPath.Row]; 

     //---- if there are no cells to reuse, create a new one 
     if (cell == null) 
     { 
      //cell = new SessionTableViewCell(new NSString(CellIdentifier), friend.FriendName, new UIImage(NSData.FromArray(friend.FriendPhoto))); 
      cell = new SessionTableViewCell(new NSString(CellIdentifier), friend); 
     } 

     //cell.UpdateCellData(friend.UserName, new UIImage(NSData.FromArray(friend.FriendPhoto))); 

     return cell; 
    } 
} 

,细胞本身

public partial class SessionTableViewCell : UITableViewCell 
{ 
    public BaseSessionController Session; 

    public SessionTableViewCell (IntPtr handle) : base (handle) 
    { 
    } 

    public SessionTableViewCell(NSString cellId, BaseSessionController session) : base(UITableViewCellStyle.Default, cellId) 
    { 
     Session = session; 
     lblDate.Text = Session.Model.SessionStartTime.ToString("d"); 
    } 
} 

希望有人可以看到我所犯的错误,并能帮助我。

在此先感谢

+0

你可以调试并检查GetCell或RowsInSection是否得到执行?或“源”不是null? – Darshana

+0

btw,故事板或xib中是否有标识符为“SessionCell”的单元格?我认为这个问题 – Darshana

+0

这两种方法都没有得到执行,我检查'source'。它绝对不是null,UITableView出口也不为空等。是的,我在Storyboard中设置标识符 –

回答

1

审查故事板,它后被发现电视表实际上是一个滚动视图。 令人沮丧的无用的错误消息:)

+0

极度无用,但非常感谢您的帮助! –

0

要注册电池的UITableView

tvTable.RegisterClassForCellReuse(typeof(SessionTableViewCell), "SessionCell"); 

更改代码

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
{ 
    SessionTableViewCell cell = (SessionTableViewCell)tableView.DequeueReusableCell(CellIdentifier); 
    BaseSessionController friend = TableItems[indexPath.Row]; 


    cell.Session = friend; 

    return cell; 
} 

public partial class SessionTableViewCell : UITableViewCell 
{ 
    private BaseSessionController _session; 
    public BaseSessionController Session 
    { 
     get { return _session; } 
     set 
     { 
      _session = value; 
      if(value != null) 
      { 
       lblDate.Text = value.Model.SessionStartTime.ToString("d"); 
      } 
     } 
    } 

    public SessionTableViewCell (IntPtr handle) : base (handle) 
    { 
    } 
} 
+0

'cell = new SessionTableViewCell();'会产生一些问题,因为我需要提供一个'IntPtr'变量 –

+0

对不起,你应该删除'if(cell = null)'看到我的回答更新 – sunyt

+0

好吧,但是我仍然在同一行得到同样的错误 –

0

你可以试试这个:

ViewDidLoad

public override void ViewDidLoad() 
{  
    base.ViewDidLoad(); 
    SessionTableViewSource source = new SessionTableViewSource(Sessions); 
    tvTable.Source = source; 
} 

在你TableViewSource - >GetCell把这个:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
{ 
    BaseSessionController friend = TableItems[indexPath.Row]; 
    var cell = tableView.DequeueReusableCell ("SessionCell") as SessionTableViewCell; 
    cell.Update(friend); 
    return cell; 
} 

而在你SessionTableViewCell把这个:

public void UpdateCell(BaseSessionController friend) 
{ 
    Session = session; 
    lblDate.Text = Session.Model.SessionStartTime.ToString("d"); 
} 
+0

它仍然崩溃与NullReferenceException:/ –

+0

@Daniel尝试更改'GetCell'只返回'新的UITableViewCell();'和看看会发生什么 – angak

+0

这只是抛出同样的异常,因为GetCell之前没有'tvTable.Source =源调用;' –