【发布时间】:2012-07-24 06:05:00
【问题描述】:
我为 UITableViewCell 编写了一个名为 TaskCell 的类,并设计了一个 .xib 文件并将 File's Owner 连接到自定义类。然后在 View Controller 类中我想加载自定义设计,但它没有显示。这是带有表格视图的视图控制器的代码:
ShowTaskViewController.h 文件:
@interface ShowTaskViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
//Database object
sqlite3 *taskeeDB;
NSString *databasePath;
}
@end
ShowTaskViewController.m 文件,在索引路径方法中包含行单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//Get data for cell
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Task *taskObj1 = (Task *)[appDelegate.taskArray objectAtIndex:indexPath.row];
NSString *sName = [[NSString alloc] initWithFormat:@"%@",taskObj1.name];
NSString *sLoc = [[NSString alloc] initWithFormat:@"%@",taskObj1.location];
NSString *sTime = [[NSString alloc] initWithFormat:@"%@",taskObj1.time];
NSString *sDate = [[NSString alloc] initWithFormat:@"%@",taskObj1.date];
//Configure cell if null
if (cell == nil) {
//I have written a custom init method to take parameters and assign label text values
cell = [[TaskCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier name:sName location:sLoc time:sTime date:sDate];
}
return cell;
}
如果出列单元格列表中的单元格为空,我将分配新的单元格对象,其中 TaskCell 是自定义 UITableViewCell 类。我在做正确的过程吗?
显示我尝试按以下方式创建单元格?
if (cell == nil){
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"TaskCell" owner:self options:nil];
for (id currentObjects in objects ) {
if ([currentObjects isKindOfClass:[UITableViewCell class]]) {
cell = (TaskCell*) currentObjects;
break;
}
}
}
【问题讨论】:
-
TaskCell *cell = (TaskCell*)[tableView dequeueReusableCellWithIdentifier... -
你是否将xib中
UITableViewCell的reuseIdentifier设置为“单元格”? -
您在
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中使用单元格标识符“Cell”。如果您创建了自定义单元格,请给它一个自定义名称。 -
我已在 .xib 文件中将自定义类设置为 TaskCell,并将重用标识符设置为“Cell”。顺便说一句,单元格的基本视图有效,如果我设置文本和副标题,它可以工作,但是当我尝试加载自定义设计时我会空白单元格。
-
您是否为重用标识符尝试了另一个名称?单元格被基本单元格使用,可能会发生冲突。
标签: iphone objective-c ios ios5 ios4