xamarin UITableViewController单元格出列

时间:2013-12-04 16:52:48

标签: ios uitableview xamarin

我有一个大约100个单元格的UITableViewController,在一个单元格中(让我们说第一个)我想画一个这样的子视图:

UILabel l = new UILabel(new RectangleF(10f, 10f, 200f, 20f));
l.Text = "test";
l.BackgroundColor = UIColor.Red;

问题是,一旦屏幕完全向上滚动,第二个屏幕上的第一个单元格仍然有视图,第3个,第4个屏幕等也是如此......

我怀疑

UITableViewCell cell = tableView.DequeueReusableCell(_section1CellId);

无法正常工作,重复使用单元格时,单元格内容不会被清理,因此重用相同视图的任何单元格都会显示它。

代码示例:

表格视图控制器

public class MyViewController : UITableViewController
{
    public MyViewController()
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        View.Frame = UIScreen.MainScreen.Bounds;
        View.BackgroundColor = UIColor.White;
        View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

        TableView.DataSource = new MainTableDataSource();
        TableView.Delegate = new MainTableDelegate(this);
        base.ViewDidLoad();
    }

}

表数据源

public class MainTableDataSource : UITableViewDataSource
{
    private List<string> _items;
    private string _section1CellId;

    public MainTableDataSource()
    {
        _section1CellId = "cellid";
        _items = new List<string>()
        {   
            "Battlewise Valor",  "Cavalry Pegasus",  "Celestial Archon",  "Chained to the Rocks",  "Chosen by Heliod",  "Dauntless Onslaught",  "Decorated Griffin",  "Divine Verdict",  "Elspeth, Sun's Champion",  "Ephara's Warden",  
            "Evangel of Heliod",  "Fabled Hero",  "Favored Hoplite",  "Gift of Immortality",  "Glare of Heresy",  "Gods Willing",  "Heliod, God of the Sun",  "Heliod's Emissary",  "Hopeful Eidolon",  "Hundred-Handed One",  
            "Lagonna-Band Elder",  "Last Breath",  "Leonin Snarecaster",  "Observant Alseid",  "Ordeal of Heliod",  "Phalanx Leader",  "Ray of Dissolution",  "Scholar of Athreos",  "Setessan Battle Priest",  "Setessan Griffin",  
            "Silent Artisan",  "Soldier of the Pantheon",  "Spear of HeliodLegendary ",  "Traveling Philosopher",  "Vanquish the Foul",  "Wingsteed Rider",  "Yoked Ox",  "Annul",  "Aqueous Form",  "Artisan of Forms",  "Benthic Giant", 
            "Bident of Thassa",  "Breaching Hippocamp",  "Coastline Chimera",  "Crackling Triton",  "Curse of the Swine",  "Dissolve",  "Fate Foretold",  "Gainsay",  "Griptide",  "Horizon Scholar",  "Lost in a Labyrinth",  
            "Master of Waves",  "Meletis Charlatan",  "Mnemonic Wall",  "Nimbus Naiad",  "Omenspeaker",  "Ordeal of Thassa",  "Prescient Chimera",  "Prognostic Sphinx",  "Sea God's Revenge",  "Sealock Monster",  "Shipbreaker Kraken", 
            "Stymied Hopes",  "Swan Song",  "Thassa, God of the SeaLegendary ",  "Thassa's Bounty",  "Thassa's Emissary",  "Triton Fortune Hunter",  "Triton Shorethief",  "Triton Tactics",  "Vaporkin",  "Voyage's End",  "Wavecrash Triton",
            "Abhorrent Overlord",  "Agent of the Fates",  "Asphodel Wanderer",  "Baleful Eidolon",  "Blood-Toll Harpy",  "Boon of Erebos",  "Cavern Lampad",  "Cutthroat Maneuver",  "Dark Betrayal",  "Disciple of Phenax",  
            "Erebos, God of the DeadLegendary ",  "Erebos's Emissary",  "Felhide Minotaur",  "Fleshmad Steed",  "Gray Merchant of Asphodel",  "Hero's Downfall",  "Hythonia the CruelLegendary ",  "Insatiable Harpy",  "Keepsake Gorgon",  
            "Lash of the Whip",  "Loathsome Catoblepas",  "March of the Returned",  "Mogis's Marauder",  "Nighthowler",  "Ordeal of Erebos",  "Pharika's Cure",  "Read the Bones",  "Rescue from the Underworld",  "Returned Centaur",  
            "Returned Phalanx",  "Scourgemark",  "Sip of Hemlock",  "Thoughtseize",  "Tormented Hero",  "Viper's Kiss",  "Whip of ErebosLegendary ",  "Akroan Crusader",  "Anger of the Gods",  "Arena Athlete",  "Borderland Minotaur",  "Boulderfall",  "Coordinated Assault",  "Deathbellow Raider",  "Demolish",  "Dragon Mantle",  "Ember Swallower",  "Fanatic of Mogis",  "Firedrinker Satyr",  "Flamespeaker Adept",  "Hammer of PurphorosLegendary ",  
            "Ill-Tempered Cyclops",  "Labyrinth Champion",  "Lightning Strike",  "Magma Jet",  "Messenger's Speed",  "Minotaur Skullcleaver",  "Ordeal of Purphoros",  "Peak Eruption",  "Portent of Betrayal",  "Priest of Iroas",  
            "Purphoros, God of the ForgeLegendary ",  "Purphoros's Emissary",  "Rage of Purphoros",  "Rageblood Shaman",  "Satyr Rambler",  "Spark Jolt",  "Spearpoint Oread",  "Stoneshock Giant",  "Stormbreath Dragon",  
            "Titan of Eternal Fire",  "Titan's Strength",  "Two-Headed Cerberus",  "Wild Celebrants",  "Agent of Horizons",  "Anthousa, Setessan HeroLegendary ",  "Arbor Colossus",  "Artisan's Sorrow",  "Boon Satyr",  
            "Bow of NyleaLegendary ",  "Centaur Battlemaster",  "Commune with the Gods",  "Defend the Hearth",  "Fade into Antiquity",  "Feral Invocation",  "Hunt the Hunter",  "Karametra's Acolyte",  "Leafcrown Dryad"
        };
    }

    public override string TitleForHeader(UITableView tableView, int section)
    {
        return "Items";
    }

    public override int RowsInSection(UITableView tableview, int section)
    {
        return _items.Count;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        // For more information on why this is necessary, see the Apple docs
        var row = indexPath.Row;

        UITableViewCell cell = tableView.DequeueReusableCell(_section1CellId);

        int n = (int) indexPath.IndexAtPosition(indexPath.Length - 1);

        if (cell == null)
        {
            // See the styles demo for different UITableViewCellAccessory
            cell = new UITableViewCell(UITableViewCellStyle.Default, _section1CellId);
            cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;


        }

        string s = "";

        s = row + ":" + cell.Subviews.Count();


        if (row.Equals(5))
        {
            UILabel l = new UILabel(new RectangleF(10f, 10f, 200f, 20f));
            l.Text = "TOTO";
            l.BackgroundColor = UIColor.Red;
            //UIView v = new UIView(new RectangleF(10f, 10f, 20f, 20f));
            //v.BackgroundColor = UIColor.Green;
            cell.Add(l);
        }

        if (row < 20)
        {

            cell.TextLabel.Text = _items[indexPath.Row];
        }

        s = s + ":" + cell.Subviews.Count();
        Console.WriteLine(s);

        return cell;
    }
}

1 个答案:

答案 0 :(得分:1)

这是正常行为。 DequeueReusableCell DequeueReusableCell时,返回的单元格可能已包含内容。您需要负责任何清理并根据新的indexPath设置单元格的值。