[英]Custom cells not loading in tableview?
我遇到了最奇怪的問題; 有誰知道為什么下面的表格視圖沒有加載我的自定義單元格? 是的,數據源和委托已連接到視圖控制器:)我之前已經做過,但是突然之間就像丟失了一些東西。
。H
@interface MyAccountViewController : UIViewController {
IBOutlet UITableView *StorageTableView;
NSArray *StoredItems;
NSMutableData *data;
}
.m
- (int)numberOfSectionsInTableView: (UITableView *)tableview
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [StoredItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *node = [StoredItems objectAtIndex:indexPath.row];
[[cell itemName] setText:[node objectForKey:@"title"]];
[[cell itemDescrip] setText:[[[[node objectForKey:@"body"] objectAtIndex:0] objectForKey:@"raw"] objectForKey:@"value"]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 99;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
您是否注冊了自定義單元以供使用?
UINib *nib = [UINib nibWithNibName:@"StorageItemTableViewCell" bundle:nil];
[self.StorageTableView registerNib:nib forCellReuseIdentifier:@"StorageItemTableViewCell"];
一個大問題是每個單元只能設置一次。 您需要將代碼更改為:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.itemName.text = [[StoredItems objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.itemDescrip.text = [[StoredItems objectAtIndex:indexPath.row] objectForKey:@"description"];
return cell;
}
您還需要確保[StoredItems count]
返回的非零值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.