MonoMobile.Views.BaseDialogViewSource.NewCell C# (CSharp) Method

NewCell() protected method

protected NewCell ( NSString cellId, NSIndexPath indexPath ) : UITableViewCell
cellId NSString
indexPath NSIndexPath
return UITableViewCell
		protected virtual UITableViewCell NewCell(NSString cellId, NSIndexPath indexPath)
		{
			var cellStyle = UITableViewCellStyle.Default;
			var cell = new UITableViewCell(cellStyle, cellId) { };			

			var views = new List<UIView>();
			var section = Sections[indexPath.Section];
		
			var key = cellId.ToString();

			if (section.ViewTypes != null && section.ViewTypes.ContainsKey(key))
			{
				var viewTypes = section.ViewTypes[key];
				if (viewTypes != null)
				{
					var memberData = GetMemberData(indexPath);

					foreach (var viewType in viewTypes)
					{
						UIView view = null;
						var hasFrameCtor = viewType.GetConstructor(new Type[] { typeof(RectangleF) }) != null;
						if (hasFrameCtor)
							view = Activator.CreateInstance(viewType, new object[] { cell.ContentView.Bounds }) as UIView;
						else
							view = Activator.CreateInstance(viewType) as UIView;

						var memberAttributes = memberData.Member.GetCustomAttributes(false);
						
						foreach (var memberAttribute in memberAttributes)
						{
							var hasCellViewTemplate = view as ICellViewTemplate;
							var cellViewTemplate = memberAttribute as CellViewTemplate;
							if (hasCellViewTemplate != null && cellViewTemplate != null && hasCellViewTemplate.GetType() == cellViewTemplate.CellViewType)
							{
								hasCellViewTemplate.CellViewTemplate = cellViewTemplate;
							}

							var viewTheme = view as IThemeable;
							if (viewTheme != null)
							{
								var memberTheme = memberAttribute as CellViewTemplate;
								if (memberTheme != null && memberTheme.Theme != null)
								{
									viewTheme.Theme = Theme.CreateTheme(memberTheme.Theme);
								}
							}

							var navigable = view as INavigable;
							if (navigable != null)
							{
								var memberNavigable = memberAttribute as INavigable;
								if (memberNavigable != null)
								{
									navigable.NavigateToViewType = memberNavigable.NavigateToViewType;
									navigable.IsModal = memberNavigable.IsModal;
									navigable.TransitionStyle = memberNavigable.TransitionStyle;
								}
							}

							var caption = view as ICaption;
							if (caption != null)
							{
								var memberCaption = memberAttribute as ICaption;
								if (memberCaption != null && !string.IsNullOrEmpty(memberCaption.Caption))
								{
									caption.Caption = memberCaption.Caption;
								}
							}
						}

						var commandButton = view as ICommandButton;
						if (commandButton != null && !string.IsNullOrEmpty(commandButton.CommandMemberName))
						{
							var commandMember = GetMemberFromView(commandButton.CommandMemberName);
							if (commandMember != null)
							{
								commandButton.Command = commandMember.GetValue(memberData.Source) as ICommand;
							}
						}

						var dc = view as IDataContext<MemberData>;
						if (dc != null)
						{
							dc.DataContext = memberData;
						}

						var initializeCell = view as IInitializeCell;
						if (initializeCell != null)
						{
							var newCellStyle = initializeCell.CellStyle;
							if (newCellStyle != cellStyle)
							{
								// recreate cell with new style
								cell = new UITableViewCell(newCellStyle, cellId) { };
							}
	
							initializeCell.Cell = cell;
							initializeCell.Controller = Controller;
						}
						
						var themeable = view as IThemeable;
						if (themeable != null)
						{
							var theme = Theme.CreateTheme(Controller.Theme);

							var themeAttribute = viewType.GetCustomAttribute<ThemeAttribute>();
							if (themeAttribute != null)
							{
								var viewTypeTheme = Theme.CreateTheme(themeAttribute.ThemeType);
								theme.MergeTheme(viewTypeTheme);
							}

							themeAttribute = memberData.Member.GetCustomAttribute<ThemeAttribute>();
							if (themeAttribute != null)
							{
								var memberTheme = Theme.CreateTheme(themeAttribute.ThemeType);
								theme.MergeTheme(memberTheme);
							}

							themeable.Theme = theme;
							themeable.Theme.Cell = cell;
						}

						var initalizable = view as IInitializable;
						if (initalizable != null)
						{
							initalizable.Initialize();
						}
						
						views.Add(view);
					}
				}
			}
			
			cell.TextLabel.Text = Caption;
			cell.TextLabel.BackgroundColor = UIColor.Clear;
			
			if (cell.DetailTextLabel != null)
			{
				cell.DetailTextLabel.BackgroundColor = UIColor.Clear;
			}
			
			var selectable = this as ISelectable;
			cell.SelectionStyle = selectable != null ? UITableViewCellSelectionStyle.Blue : UITableViewCellSelectionStyle.Blue;
			
			if (views.Count > 0)
			{
				cell.ContentView.AutosizesSubviews = true;
			}

			section.Views.Add(cell, views);
			var resizedRows = false;
			
			foreach (var view in views)
			{
				var accessoryView = view as IAccessoryView;
				if (accessoryView != null)
				{
					view.Tag = 1;
					view.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleLeftMargin;
					cell.AccessoryView = view;
				}
				else
				{
					var contentView = view as ICellContent;
					if (contentView != null)
					{
						view.Tag = 1;
						view.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleLeftMargin;
						cell.ContentView.Add(view);
					}
				}
				
				var sizeable = view as ISizeable;
				if (sizeable != null)
				{
					var rowHeight = sizeable.GetRowHeight();

					if (RowHeights.ContainsKey(indexPath))
					{
						if (RowHeights[indexPath] != rowHeight)
						{
							RowHeights[indexPath] = rowHeight;
							resizedRows = true;
						}
					}
					else
					{
						RowHeights.Add(indexPath, rowHeight);
						resizedRows = true;
					}
				}
			}

			if (resizedRows)
			{
				new Wait(TimeSpan.FromMilliseconds(0), () =>
				{
					Controller.TableView.BeginUpdates();
					Controller.TableView.EndUpdates();
				});
			}

			return cell;
		}