Ios UITableView Custon单元不工作
尝试使用自定义单元格创建表视图。 我看了一些教程,但没有开始一个新的项目,而是一步一步地进行。 我试着用我现在的那个。这样我就可以解决问题并了解更多信息 以下是我到目前为止所做的步骤: 简单表格视图: (WorkoutList.xib) WorkoutList.h(workoutTableView是您在上图中看到的视图) WorkoutList.m:Ios UITableView Custon单元不工作,ios,iphone,uitableview,Ios,Iphone,Uitableview,尝试使用自定义单元格创建表视图。 我看了一些教程,但没有开始一个新的项目,而是一步一步地进行。 我试着用我现在的那个。这样我就可以解决问题并了解更多信息 以下是我到目前为止所做的步骤: 简单表格视图: (WorkoutList.xib) WorkoutList.h(workoutTableView是您在上图中看到的视图) WorkoutList.m: - (void)viewDidLoad { [super viewDidLoad]; self.workoutTableVi
- (void)viewDidLoad
{
[super viewDidLoad];
self.workoutTableView.dataSource = self;
self.workoutTableView.delegate = self;
TitleArray = [[NSArray alloc] initWithObjects:@"First", @"Second", @"Third", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return TitleArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[WorkoutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.workoutTitle.text = [TitleArray objectAtIndex:indexPath.row];
cell.workoutImage.image = [UIImage imageNamed:@"list-item-icon3"];
return cell;
}
WorkoutCell.xib:
自定义类为:WorkoutCell
标识符是:Cell
工作单元.h:
我所看到的只是一个空的桌面视图
我知道这是一个很长的问题,但对我来说,理解它并了解我的错误是非常重要的。非常感谢 在viewcontroller.h文件中,确保在界面中添加如下内容:
@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@接口myViewController:UIViewController
此外,在.xib文件中,选择UITableView并连接数据源,并将其委托给文件所有者,将tableView委托给workoutTableView,如下面的屏幕截图所示:
最后,在WorkoutCell xib中,选择文件所有者并将视图控制器添加到自定义类。此处无需创建.h和.m文件。只需创建WorkoutCell.xib文件。然后在文件检查器(RHS)中设置UIImageview(标记1)和UILabel(标记2)的标记值。然后在CellForRowatineIndexPath中编写以下代码
static NSString *MyIdentifier = @"WorkoutCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// If no cell is available, create a new one using the given identifier.
if (cell == nil) {
// Use the default cell style.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WorkoutCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
UIImageView *img = (UIImageView *) [cell viewWithTag:1];
[img setImage:[UIImage imageNamed:@"your image"]];
UILabel *lbl=(UILabel *)[cell viewWithTag:2];
[lbl setText:@"your Text"];
}
}
此外,您还有TitleArray.count,就好像它是一个属性一样。count是数组上的一个方法。您需要按如下所示调用它
return [TitleArray count];
什么是“不工作”?没有放我去访问链接请。描述一下!我只是写了所有关于我的问题的东西,包括代码和图像..很好,比你写的要多..你在interface builder中将tableview的类设置为自定义类了吗?