无法在iOS中使用动态单元格显示tableview

时间:2012-11-09 20:50:21

标签: ios xcode uitableview

我有一个nslog,它成功地告诉我,我将加载到动态单元格中的信息就在那里。但是,我发现我的tableView没有加载带有对象“dogs”的数组“dogs”,因为name11Label的nslog返回null,尽管viewDidLoad中有一个dog值。启动tableView需要做什么? (.h确实有一个iboutlet和一个属性(只是为了确保)为我的“tableView”,并且还具有和导入cell11.h)

.m文件

-(void)viewDidLoad{
...
     self.tableview.delegate = self;
...
}

    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
        if ( dogs != NULL ) {
            return [dogs count];
        }
        return 0;
    }

    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       Cell11 *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell11"];

        if (cell == nil)
        {
            cell = [[[Cell11 alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell11"] autorelease];  //I know that this method is depreciated, but it is not the source of this problem
        }
        NSDictionary *itemAtIndex =(NSDictionary *)[dogs objectAtIndex:indexPath.row];
         cell.name11Label.text = [itemAtIndex objectForKey:@"dogs"];
         NSLog(@"dogs array = %@", dogs);   //returns correct information of the object dogs
         NSLog(@"%@", cell.name11Label.text);   //returning null even though "dogs" in viewDidLoad is showing a result;

         return cell;

    }

Cell11.h

#import <UIKit/UIKit.h>

@interface Cell11 : UITableViewCell
@property (nonatomic, strong) IBOutlet UILabel *name11Label;

@end

Cell11.m

#import "Cell11.h"

@implementation Cell11
@synthesize name11Label;

@end

3 个答案:

答案 0 :(得分:0)

您是否已使用UITableViewDataSource协议(上述方法)将UITableView的“dataSource”属性分配给对象?

答案 1 :(得分:0)

变化:

Cell11 *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell11"];

Cell11 *cell = [tv dequeueReusableCellWithIdentifier:@"Cell11"];

tableView可能没有正确连接。

<击> 永远不会调用nslog语句。将其移到上方:

NSLog(@"%@", cell.name11Label);
return cell;

此外,您正在使用cgrect零初始化单元格,这将不显示任何内容。确保它具有预期的高度和宽度(例如CGRectMake(0.0f,0.0f,320.0f,50.0f))

如果单元格仍未显示,是否在设置数组狗后在tableview上调用reloadData?

答案 2 :(得分:0)

首先:永远不会调用返回后的NSLog。

    NSLog(@"%@", cell.name11Label); 
    return cell;
}

第二:控制从界面构建器的tableview到您的类的拖动。并确保连接dataSource已正确连接。