Ios UITableViewSource单元格高度始终为44点
我正在为源于UITableViewSource的UITableView编写自定义单元格。我已经实现了GetHeightForRow并记录了结果,以确保高度正确通过。问题是,无论GetHeightForRow返回的值是什么,我的自定义单元格的帧高度都是固定的44。副作用当然是我的层边界不正常,UITableView会切断我最后一个单元格的底部,不允许再滚动 值得一提的是,我曾尝试在我的单元格构造函数逻辑和GetCell逻辑中手动更改单元格的帧,但对大多数人来说,这并不重要,使用的值总是恢复为44 以下是有关我的UITableViewSource逻辑的一些想法:Ios UITableViewSource单元格高度始终为44点,ios,uitableview,xamarin.ios,Ios,Uitableview,Xamarin.ios,我正在为源于UITableViewSource的UITableView编写自定义单元格。我已经实现了GetHeightForRow并记录了结果,以确保高度正确通过。问题是,无论GetHeightForRow返回的值是什么,我的自定义单元格的帧高度都是固定的44。副作用当然是我的层边界不正常,UITableView会切断我最后一个单元格的底部,不允许再滚动 值得一提的是,我曾尝试在我的单元格构造函数逻辑和GetCell逻辑中手动更改单元格的帧,但对大多数人来说,这并不重要,使用的值总是恢复为44
public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
var result = SingleItemCell.GetHeight(_models[indexPath.Row], _width, _isStandalone));
Console.WriteLine(result); //prints various heights, never 44
return result;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell(Id) as SingleItemCell ??
(_isStandalone ? new StandaloneItemCell() : new SingleItemCell());
var model = _models[indexPath.Row];
cell.Model = model;
Console.WriteLine(cell.Frame.Height); //always 44
return cell;
}
更新
有趣的是,单元格分隔符显示在正确的位置,但我的自定义视图仍然无法看到正确的帧高度
即使更改自定义单元格视图的构造函数以将显式框架传递给基础构造函数,框架的高度也不会更改
public SingleOffer(RectangleF frame)
: base(frame) //example new RectangleF(0, 0, 300, 65)
{
//Here, before custom logic, Frame is still 0, 0, 300, 44!
Initialize();
}
实现tableView:heightForRowAtIndexpath:以返回所需的tableView单元格高度。如果不实现此方法,框架默认高度为44。我无法重现您的问题。正如您所理解的,关键是
UITableViewSource.GetHeightForRow
。以下程序创建预期输出:
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace SingleFileTableViewSolution
{
public class DomainClass
{
static Random rand = new Random (0);
public UIColor Color { get; protected set; }
public float Height { get; protected set; }
static UIColor[] Colors = new UIColor[]
{
UIColor.Red,
UIColor.Green,
UIColor.Blue,
UIColor.Yellow
};
public DomainClass ()
{
Color = Colors[rand.Next(Colors.Length)];
switch(rand.Next(3))
{
case 0 :
Height = 24.0f;
break;
case 1 :
Height = 44.0f;
break;
case 2 :
Height = 64.0f;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
//Table Source
public class ColorTableDataSource : UITableViewSource
{
List<DomainClass> Model { get; set; }
public ColorTableDataSource(List<DomainClass> model)
{
this.Model = model;
}
public override int RowsInSection(UITableView tableView, int section)
{
return Model.Count;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell(ColoredTableCell.ID);
if(cell == null)
{
cell = new ColoredTableCell();
}
cell.ContentView.BackgroundColor = Model[indexPath.Row].Color;
return cell;
}
public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
var height = Model[indexPath.Row].Height;
return height;
}
}
public class ColoredTableCell : UITableViewCell
{
public static readonly string ID = "ColoredTableCell";
public ColoredTableCell ()
{
}
}
public class ColorTableController : UITableViewController
{
String title;
public ColorTableController (String title, UITableViewSource source) : base ()
{
this.title = title;
this.TableView.Source = source;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
Title = title;
}
}
[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
UIWindow window;
ColorTableController viewController;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
var models = new List<DomainClass>();
for(int i = 0; i < 20; i++)
{
models.Add(new DomainClass());
}
var tableController = new ColorTableController("My Table", new ColorTableDataSource(models));
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = tableController;
window.MakeKeyAndVisible ();
return true;
}
}
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args, null, "AppDelegate");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用单调的基础;
使用MonoTouch.UIKit;
名称空间SingleFileTableViewSolution
{
公共类DomainClass
{
静态随机随机随机数=新随机数(0);
公共UIColor{get;protected set;}
公共浮动高度{get;protected set;}
静态UIColor[]颜色=新UIColor[]
{
颜色,红色,
绿色,绿色,
蓝色,蓝色,
颜色,黄色
};
公共域类()
{
颜色=颜色[rand.Next(Colors.Length)];
开关(随机下一个(3))
{
案例0:
高度=24.0f;
打破
案例1:
高度=44.0f;
打破
案例2:
高度=64.0华氏度;
打破
违约:
抛出新ArgumentOutOfRangeException();
}
}
}
//表源
公共类ColorTableDataSource:UITableViewSource
{
列表模型{get;set;}
公共ColorTableDataSource(列表模型)
{
这个模型=模型;
}
公共覆盖int RowsInSection(UITableView表格视图,int section)
{
返回模型.计数;
}
公共覆盖UITableViewCell GetCell(UITableView tableView,NSIndexPath indexPath)
{
var cell=tableView.DequeueReusableCell(ColoredTableCell.ID);
if(单元格==null)
{
单元格=新的彩色表格单元格();
}
cell.ContentView.BackgroundColor=Model[indexPath.Row].Color;
返回单元;
}
公共覆盖浮点GetHeightForRow(UITableView tableView,NSIndexPath indexPath)
{
var height=Model[indexPath.Row].height;
返回高度;
}
}
公共类ColoredTableCell:UITableViewCell
{
公共静态只读字符串ID=“ColoredTableCell”;
公共彩色表单元格()
{
}
}
公共类ColorTableController:UITableViewController
{
字符串标题;
公共ColorTableController(字符串标题,UITableViewSource源):基()
{
this.title=标题;
this.TableView.Source=源;
}
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
头衔=头衔;
}
}
[注册(“AppDelegate”)]
公共类AppDelegate:UIApplicationDelegate
{
窗口;
彩色表格控制器;
公共覆盖bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
{
var models=新列表();
对于(int i=0;i<20;i++)
{
models.Add(newdomainclass());
}
var tableController=new ColorTableController(“我的表”,new ColorTableDataSource(models));
窗口=新的UIWindow(UIScreen.MainScreen.Bounds);
window.RootViewController=tableController;
window.MakeKeyAndVisible();
返回true;
}
}
公共类应用程序
{
静态void Main(字符串[]参数)
{
Main(args,null,“AppDelegate”);
}
}
}
我不知道GetHeightForRow,但UITableViewDelegate知道。看起来它是MonoTouch的翻译方法签名。我还尝试将我的UITableViewSource分离为UITableViewDataSource和UITableViewDelegate,得到了相同的结果。您是否将UITableViewDelegate(设置为self)了?您可能看到UITableViewDataSource调用该方法,而不是UITableViewDelegate。这里的问题是,GetHeightForRow
用于表视图委托
,而不是数据源
。我不确定是否完全理解您的任何注释,但是UITableViewSource是UITableViewDataSource和UITableViewDelegate的组合。我尝试了组合和分离的方法(UITableView.Delegate/UITableView.DataSource&UITableView.Source),两者都有相同的结果……构建自定义视图时,帧高度为44:(Monotouch的GetHeightForRow与tableView相同:HeightForRow在IndexPath中,根据我的日志记录,我肯定我永远不会返回44。他只是试图回答,所以我认为他不应该得到否定的一点。你没有提到Monotouch。我被误导了,甚至试图在谷歌上搜索GetHeightForRow,因为我试图在不同的平台上解决类似的问题 context@Zennichimaro,并不是我给出了否定的观点,我的评论只是澄清一下,以防万一。