XIBファイルをロードするかコードでビューにUIを作成しますか?
「XIBファイルをロードする」場合、UIViewController継承で準備されたUIを使用するのは少し面倒です。
「コードによる」場合は、次のことを再確認してください。
- AnnouncementPanelに属するコントロールを作成します。
- AddSubviewを呼び出して、AnnouncementPanel.Viewにコントロールを追加します。
- コントロールフレームを正しく設定します(画面の表示部分)。
- コールベース(つまりSurveyPanel)が作成を制御します。
(UITableViewCells)ビューの継承に使用する例:
// Base cell
public class UserCell: UIViewController
{
string cellId = "CustomCell2";
UITableViewCell cell;
public UITableViewCell Cell {
get {
if (cell == null)
cell = new UITableViewCell (UITableViewCellStyle.Default, cellId);
return cell; }
}
protected UIImageViewAvatar IvaAvatar;
protected UILabel LblPerson;
...
public UserCell (string cellId)
{
this.cellId = cellId;
IvaAvatar = new UIImageViewAvatar ();
LblPerson = new UILabel ();
Cell.AddSubview (IvaAvatar);
Cell.AddSubview (LblPerson);
}
...
}
// Inherited cell
public class MessageCell: UserCell
{
protected UILabel LblText;
protected UILabel LblDatePlace;
Message message;
public Message Message {
get { return message; }
set {
message = value;
Render ();
}
}
...
// 4.
// It is important to call base constructor
public MessageCell (string cellId): base(cellId)
{
// 1.
LblText = new UILabel ();
LblDatePlace = new UILabel ();
// 2.
// Adding more controls to (Cell) View
Cell.AddSubview (LblText);
Cell.AddSubview (LblDatePlace);
}
...
// 3.
// Render is called when someone set up Message property
// So frames will be changed according to new data
public new void Render()
{
base.Render();
// Text
LblText.Text = Message.Text;
LblText.Font = UIFont.SystemFontOfSize (Constants.FONT_SIZE_MESSAGE);
LblText.LineBreakMode = UILineBreakMode.WordWrap;
LblText.Lines = 0;
LblText.Frame = new RectangleF (LblPerson.Frame.Left, LblPerson.Frame.Bottom //+ Constants.SIZE_IDENT*/
,
Cell.Frame.Width - LblPerson.Frame.Left - Constants.SIZE_IDENT, 0);
LblText.SizeToFit ();
}
}