IB에서 내 자신의 UITableViewCell을 디자인 할 수 있기를 원합니다. 하지만 IB에서 정의한 레이블에 액세스하려고 할 때 null ref 예외가 계속 발생합니다.Interface Builder에서 사용자 정의 UITableViewCell을 사용하는 방법은 무엇입니까?
여기에 내가 뭘 내용은 다음과 같습니다 인터페이스 빌더에서
:
- 내가 "보기"를 제거하고 대신있는 UITableViewCell을 추가했다.
- UITableViewCell의 클래스를 "
TestCellView
"으로 변경했습니다. - 셀에 UILabel이 추가되었습니다.
- 콘센트 "
oLblText
"~TestCellView
을 추가하고 UILabel에 연결합니다. - 클래스 식별자를 "TestCellView"로 변경했습니다.
TestCellView.xib.cs
public partial class TestCellView : UITableViewCell
{
public TestCellView(string sKey) : base(UITableViewCellStyle.Default, sKey)
{
}
public TestCellView(IntPtr oHandle) : base(oHandle)
{
}
public string TestText
{
get
{
return this.oLblText.Text;
}
set
{
// HERE I get the null ref exception!
this.oLblText.Text = value;
}
}
}
을 구현 ** TestCellView.designer.cs ** 내 테이블의 소스에서
[MonoTouch.Foundation.Register("TestCellView")]
public partial class TestCellView {
private MonoTouch.UIKit.UILabel __mt_oLblText;
#pragma warning disable 0169
[MonoTouch.Foundation.Connect("oLblText")]
private MonoTouch.UIKit.UILabel oLblText {
get {
this.__mt_oLblText = ((MonoTouch.UIKit.UILabel)(this.GetNativeField("oLblText")));
return this.__mt_oLblText;
}
set {
this.__mt_oLblText = value;
this.SetNativeField("oLblText", value);
}
}
}
:
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
TestCellView oCell = (TestCellView)tableView.DequeueReusableCell("myCell");
if(oCell == null)
{
// I suppose this is wrong but how to do it correctly?
// this == my UITableViewSource.
NSBundle.MainBundle.LoadNib("TestCellView", this, null);
oCell = new TestCellView("myCell");
}
oCell.TestText = "Cell " + indexPath.Row;
return oCell;
}
모든 셀에 대해 UIViewController가 포함 된 솔루션은 필요하지 않습니다. 이 작업을 수행하는 웹에서 몇 가지 예를 보았습니다. 나는 그것이 총 과잉이라고 생각한다.
내가 뭘 잘못하고 있니?
그게 정확히 지금 가지고있는 방법입니다. 나는 컨트롤러가있는 것을 좋아하지 않는다. 그 이유는 기본 셀 스타일을 사용할 때 컨트롤러가 없기 때문입니다. 그래서 애플은 컨트롤러가 필요 없다고 결정했다. 맞춤형 셀을 만들 때 셀 당 하나의 컨트롤러가 필요합니다. – Krumelur
예 기본 셀 스타일을 사용하는 경우 프로그래밍 방식으로 셀을 만들므로 컨트롤러가 필요하지 않습니다. 하지만 NIB 파일에서로드되는 일반적으로 사용자 인터페이스 개체 인 셀을 사용하려고합니다. NIB 파일에 사용자 정의 셀을 놓는 것만으로는 충분하지 않습니다. 그것은 FileViewer와 어떻게 든 연결되어야합니다.이 경우에는 UIViewController 여야합니다. –