簡體   English   中英

表格視圖未顯示自定義單元格

[英]Table view doesn't show up custom cells

我有一個視圖控制器,當單擊一個按鈕時,它會顯示一個表視圖控制器。 一切都可以使用表視圖委托正常運行,表視圖可以正常顯示,但是在ellForRowAtIndexPath:委托方法中,實例化並返回了單元格,但未正確顯示。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"alrededor";

    alrededorCell *cell = [tableView 
                           dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];

    NSLog(@"categoria %@", categoria);

    cell.title.text = [categoria valueForKey:@"title"];

    return cell;

}

非常感謝

為什么要像這樣創建單元格?

if (cell == nil) {
    cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];
}

如果這是您自己的自定義單元格,那么為什么要使用UITableViewCellStyleDefault?

如果要加載的單元格是UITableViewCell的子類,並且已使用接口構建器來構建該單元格,則必須做幾件事。

在nib文件中,刪除創建視圖時所在的視圖,添加UITableViewCell並將其類更改為alrededorCell。 更改單元格類,而不是文件的所有者。 當您鏈接按鈕,標簽等時。 確保將它們鏈接到單元而不是文件的所有者。 還將單元格的uniqueIdentifier設置為alrededor。

在cellForRowAtIndexPath中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"alrededor";

    alrededorCell *cell = [tableView 
                       dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:nil options:nil];
        for (alrededorCell *view in xib) {
            if ([view isKindOfClass:[alrededorCell class]]) {
                cell = view;
            }
        }
    }


    NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];

    NSLog(@"categoria %@", categoria);

    cell.title.text = [categoria valueForKey:@"title"];

    return cell;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM