繁体   English   中英

UITableViewSource单元格高度始终为44点

[英]UITableViewSource Cells Height Always 44 Points

我正在为来自UITableViewSource的UITableView编写一个自定义单元格。 我已经实现了GetHeightForRow并记录了结果以确保高度正确。 问题是,我的自定义单元格的框架高度是固定的44,无论从GetHeightForRow返回什么值。 副作用当然是我的图层边界是不正常的,UITableView切断了我最后一个单元格的底部,并且不允许进一步滚动。

值得一提的是我尝试在我的单元构造函数逻辑和GetCell逻辑中手动更改单元格的框架,但对大多数人来说显而易见,这无关紧要,使用的值总是恢复为44。

以下是我的UITableViewSource逻辑的概念:

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;
}

UPDATE

有趣的是,单元格分隔符显示在正确的位置,但我的自定义视图仍然无法看到正确的帧高度。

即使在更改自定义单元格视图的构造函数以将显式框架传递给基础构造函数之后,我的框架的高度也不会更改。

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();
}

我无法重现你的问题。 你似乎明白,关键是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");
    }
}
}

实现tableView:heightForRowAtIndexpath:返回所需的tableviewcell高度。 如果未实现此方法,则默认情况下框架的高度为44。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM