【问题标题】:Custom cell not showing up in tableView自定义单元格未显示在 tableView 中
【发布时间】:2012-06-06 14:38:53
【问题描述】:

我有一个带有标识符“tweetCell”的自定义单元格,我已将其导入到我的 tableviewcontroller.h 文件中。我已将该类链接到情节提要中的原型单元。所有的 UILabel 都连接到单元格,但我无法让 tableview 显示自定义单元格。它起作用了吗?然后一夜之间停了?!!我知道类文件需要以大写字母开头,我已经改变了这一点,但无济于事。谁能在这里发现我的错误?先谢谢了……

- (void)fetchTweets
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"http://search.twitter.com/search.json?q=%23mysearchhashtag"]];

        NSError* error;

        tweets = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tweets.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"tweetCell";

    customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];

    NSString *tweetText = [tweet valueForKey:@"text"];

    NSArray *tweetComponents = [tweetText componentsSeparatedByString:@":"]; 


    cell.firstDetail.text = [tweetComponents objectAtIndex:0];
    cell.secondDetail.text = [tweetComponents objectAtIndex:1];
    cell.thirdDetail.text = [tweetComponents objectAtIndex:2];




    return cell;
}

【问题讨论】:

  • 如果您使用情节提要,您应该删除 if (cell == nil) { ... } 部分。故事板中的表格视图会自动为您创建一个单元格,因此 dequeue 调用将始终返回一个单元格。如果您删除了该代码并收到异常,您将知道您的单元格标识符不匹配。

标签: cocoa-touch uitableview storyboard


【解决方案1】:

这个方法- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView是你写的吗?像这样试试。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;

    }

【讨论】:

  • 没有必要。这是一种可选方法。如果它不存在,则 tableView 默认使用 1 部分。
  • 嗨,matthias,我已经更改了 if (cell == nil) { ... } 部分,但不幸的是它仍然无法正常工作
  • 我不知道它是否能说明情况,但 CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];收到绿色消息“线程 1:信号 SIGABRT”
猜你喜欢
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 2019-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多