gpt4 book ai didi

ios - xamarin UITableViewController 单元格出队

转载 作者:行者123 更新时间:2023-11-29 03:26:40 25 4
gpt4 key购买 nike

我有一个包含大约 100 个单元格的 UITableViewController,在其中一个单元格(假设是第一个)中,我想绘制一个如下所示的 subview :

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

问题在于,一旦屏幕完全向上滚动,第二个屏幕上的第一个单元格仍然具有 View ,第三个、第四个屏幕上的情况也是如此......

我怀疑

UITableViewCell cell = tableView.DequeueReusableCell(_section1CellId);

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

代码示例:

表格 View Controller

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

最佳答案

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

关于ios - xamarin UITableViewController 单元格出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20381259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com