2012-06-01 69 views
0

我有一个自定义单元格,标识符为'tweetCell'我已经将它导入到我的tableviewcontroller.h文件中。我已经将该类与故事板中的原型单元相关联。所有的UILabel连接到单元格,但我无法使用tableview来显示自定义单元格。它有用吗?然后过夜停下来?我知道类文件需要以大写字母开头,我已经改变了这一点,但无济于事。任何人都可以在此发现我的错误在此先感谢......自定义单元格不显示在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; 
} 
+1

如果您使用故事板,您应该删除'if(cell == nil){...}'部分。故事板中的桌面视图自动为您创建一个单元格,因此出列调用将始终返回一个单元格。如果您删除了该代码,并且出现异常,您将知道您的单元格标识符不匹配。 –

回答

1

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

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

    } 
+1

没有必要。这是一种可选方法。如果它不存在,tableView默认使用1节。 –

+0

嗨matthias,我已经改变了if(cell == nil){...}的一部分,但它仍然不起作用 – Alan

+0

我不知道它是否揭示了情况,但CustomCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];得到绿色的消息“线程1:信号SIGABRT” – Alan

相关问题