亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從XIB文件加載自定義UITableViewCells?

如何從XIB文件加載自定義UITableViewCells?

慕標琳琳 2019-07-04 18:32:58
如何從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;}


查看完整回答
反對 回復 2019-07-04
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

寄存器

在iOS 7之后,這個過程被簡化為(SWIFT 3.0):

// For registering nib filestableView.register(UINib(nibName: "MyCell", bundle: Bundle.main), forCellReuseIdentifier: "cell")
// For registering classestableView.register(MyCellClass.self, forCellReuseIdentifier: "cell")

()也可以通過在.xib.stroyboard文件,作為原型單元格。如果需要將類附加到它們,可以選擇單元格原型并添加相應的類(必須是UITableViewCell當然)。

去隊列

然后,使用(SWIFT 3.0):

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

當然,單元格的關聯類的類型是您在.xib文件中為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    }}

當然,這在objc中都有相同的名稱。


查看完整回答
反對 回復 2019-07-04
  • 3 回答
  • 0 關注
  • 773 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號