如何從XIB文件加載自定義UITableViewCells?問題很簡單:如何加載自定義UITableViewCell從XIB文件?這樣做可以讓您使用InterfaceBuilder來設計單元格。顯然,由于內存管理問題,答案并不簡單。這條線提到了這個問題,并提出了解決方案,但在NDA發布之前,缺乏代碼。這是一個長螺紋這討論了這個問題,但沒有給出明確的答案。下面是我使用過的一些代碼:static NSString *CellIdentifier = @"MyCellIdentifier";MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (MyCell *)[nib objectAtIndex:0];}若要使用此代碼,請創建MyCell.m/.h,這是UITableViewCell加上IBOutlets你想要的組件。然后創建一個新的“空XIB”文件。在IB中打開XIB文件,添加UITableViewCell對象,將其標識符設置為“MyCellIdentifier”,并將其類設置為MyCell并添加組件。最后,連接IBOutlets到部件上。注意,我們沒有在IB中設置文件的所有者。其他方法主張設置文件所有者,并警告如果XIB未通過額外的工廠類加載,則會發生內存泄漏。我在“儀器/泄漏”下測試了上面的內容,沒有發現內存泄漏。那么,從XBS加載電池的標準方法是什么呢?我們設置了文件的所有者嗎?我們需要工廠嗎?如果是的話,工廠的代碼是什么樣子的?如果有多種解決方案,讓我們澄清每一個方案的利弊.
3 回答
慕蓋茨4494581
TA貢獻1850條經驗 獲得超11個贊
- (void)viewDidLoad{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:@"ItemCell" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:@"ItemCell"];}-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Create an instance of ItemCell
PointsItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];
return cell;}
慕運維8079593
TA貢獻1876條經驗 獲得超5個贊
寄存器
// For registering nib filestableView.register(UINib(nibName: "MyCell", bundle: Bundle.main), forCellReuseIdentifier: "cell") // For registering classestableView.register(MyCellClass.self, forCellReuseIdentifier: "cell")
(注
)也可以通過在 .xib或 .stroyboard文件,作為原型單元格。如果需要將類附加到它們,可以選擇單元格原型并添加相應的類(必須是 UITableViewCell當然)。
去隊列
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Hello"
return cell}if (cell == nil)
(警告)
tableView.dequeueReusableCell(withIdentifier:for:)有新的行為,如果你調用另一個(沒有 indexPath:)你得到了舊的行為,在這種行為中你需要檢查 nil如果你自己動手的話,注意到 UITableViewCell?返回值
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MyCellClass{
// Cell be casted properly
cell.myCustomProperty = true}else{
// Wrong type? Wrong identifier?}UITableViewCell
配置
cellForRowAtIndexPath
合在一起
class MyCell : UITableViewCell{
// Can be either created manually, or loaded from a nib with prototypes
@IBOutlet weak var labelSomething : UILabel? = nil}class MasterViewController: UITableViewController {
var data = ["Hello", "World", "Kinda", "Cliche", "Though"]
// Register
override func viewDidLoad()
{
super.viewDidLoad()
tableView.register(MyCell.self, forCellReuseIdentifier: "mycell")
// or the nib alternative
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return data.count }
// Dequeue
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! MyCell
cell.labelSomething?.text = data[indexPath.row]
return cell }}- 3 回答
- 0 關注
- 785 瀏覽
添加回答
舉報
0/150
提交
取消
