UITableViewCell 标题不显示
TableViewCell Title not showing
我的 UITableCell
标题在我的 viewController
中没有显示。
这是怎么回事?
我已将 TableView
和 TableViewCell
拖到上面,但它没有显示,它保持白色。
截图:https://blazor.nl/uploads/get/19d2ed8a2662039e1104474b0426f2e0/IMG-0286
我用过下面的代码
-(void) arraySetup{
imageNameArray = [NSMutableArray arrayWithArray:@[@"image1", @"image2", @"image3", @"image4"]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return imageNameArray.count;
}
#pragma mark - UITableView DataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
return cell;
}
请教我怎么做?
您可以在表格视图中使用 titleForHeaderInSection :
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Your tittle here" }
这很明显,因为您正在使 table 视图单元格出列(基本上, 模板),但您没有用数据填充它。所以:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cell";
// Fix: Use dequeueReusableCellWithIdentifier:forIndexPath: instead
// of dequeueReusableCellWithIdentifier: alone. The new method
// ensures you won't get a nil cell and, thus, crashing your app.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
// Grab your data here
NSString *theTitle = imageNameArray[indexPath.row];
// And set the title of your cell
cell.textLabel.text = theTitle;
return cell;
}
您没有在 tableView 的 cellForRowAt 中的任何地方使用您的 imageNameArray,这就是您的数据未显示的原因。您正确地使 tableView 出列,但没有提供任何要显示的数据。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
CellLabel = (UILabel*) [ cell.contentView viewWithTag:2];
CellLabel = [imageNameArray objectAtIndex:indexPath.row];
return cell;
}
如果您使用默认的 tableView
然后你可以插入
cell.textLabel.text = [imageNameArray objectAtIndex:indexPath.row];
我的 UITableCell
标题在我的 viewController
中没有显示。
这是怎么回事?
我已将 TableView
和 TableViewCell
拖到上面,但它没有显示,它保持白色。
截图:https://blazor.nl/uploads/get/19d2ed8a2662039e1104474b0426f2e0/IMG-0286
我用过下面的代码
-(void) arraySetup{
imageNameArray = [NSMutableArray arrayWithArray:@[@"image1", @"image2", @"image3", @"image4"]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return imageNameArray.count;
}
#pragma mark - UITableView DataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
return cell;
}
请教我怎么做?
您可以在表格视图中使用 titleForHeaderInSection :
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Your tittle here" }
这很明显,因为您正在使 table 视图单元格出列(基本上, 模板),但您没有用数据填充它。所以:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cell";
// Fix: Use dequeueReusableCellWithIdentifier:forIndexPath: instead
// of dequeueReusableCellWithIdentifier: alone. The new method
// ensures you won't get a nil cell and, thus, crashing your app.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
// Grab your data here
NSString *theTitle = imageNameArray[indexPath.row];
// And set the title of your cell
cell.textLabel.text = theTitle;
return cell;
}
您没有在 tableView 的 cellForRowAt 中的任何地方使用您的 imageNameArray,这就是您的数据未显示的原因。您正确地使 tableView 出列,但没有提供任何要显示的数据。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
CellLabel = (UILabel*) [ cell.contentView viewWithTag:2];
CellLabel = [imageNameArray objectAtIndex:indexPath.row];
return cell;
}
如果您使用默认的 tableView 然后你可以插入
cell.textLabel.text = [imageNameArray objectAtIndex:indexPath.row];