第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

慕標(biāo)琳琳 2019-07-04 18:32:58
如何從XIB文件加載自定義UITableViewCells?問題很簡單:如何加載自定義UITableViewCell從XIB文件?這樣做可以讓您使用InterfaceBuilder來設(shè)計單元格。顯然,由于內(nèi)存管理問題,答案并不簡單。這條線提到了這個問題,并提出了解決方案,但在NDA發(fā)布之前,缺乏代碼。這是一個長螺紋這討論了這個問題,但沒有給出明確的答案。下面是我使用過的一些代碼: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];}若要使用此代碼,請創(chuàng)建MyCell.m/.h,這是UITableViewCell加上IBOutlets你想要的組件。然后創(chuàng)建一個新的“空XIB”文件。在IB中打開XIB文件,添加UITableViewCell對象,將其標(biāo)識符設(shè)置為“MyCellIdentifier”,并將其類設(shè)置為MyCell并添加組件。最后,連接IBOutlets到部件上。注意,我們沒有在IB中設(shè)置文件的所有者。其他方法主張設(shè)置文件所有者,并警告如果XIB未通過額外的工廠類加載,則會發(fā)生內(nèi)存泄漏。我在“儀器/泄漏”下測試了上面的內(nèi)容,沒有發(fā)現(xiàn)內(nèi)存泄漏。那么,從XBS加載電池的標(biāo)準(zhǔn)方法是什么呢?我們設(shè)置了文件的所有者嗎?我們需要工廠嗎?如果是的話,工廠的代碼是什么樣子的?如果有多種解決方案,讓我們澄清每一個方案的利弊.
查看完整描述

3 回答

?
慕蓋茨4494581

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超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;}


查看完整回答
反對 回復(fù) 2019-07-04
?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超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文件,作為原型單元格。如果需要將類附加到它們,可以選擇單元格原型并添加相應(yīng)的類(必須是UITableViewCell當(dāng)然)。

去隊列

然后,使用(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}

不同之處在于,這個新方法不僅將單元格排出隊列,還會創(chuàng)建不存在的情況(這意味著您不必這樣做)。if (cell == nil),該單元格已準(zhǔn)備好使用,如上面的示例所示。

(警告tableView.dequeueReusableCell(withIdentifier:for:)有新的行為,如果你調(diào)用另一個(沒有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?}

當(dāng)然,單元格的關(guān)聯(lián)類的類型是您在.xib文件中為UITableViewCell子類,或者使用其他寄存器方法。

配置

理想情況下,您的單元格在注冊時已經(jīng)在外觀和內(nèi)容定位(如標(biāo)簽和圖像視圖)和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    }}

當(dāng)然,這在objc中都有相同的名稱。


查看完整回答
反對 回復(fù) 2019-07-04
  • 3 回答
  • 0 關(guān)注
  • 784 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號