如何使用 objective C 显示多个自定义单元格?
How do I show multiple custom cells using objective C?
我有几个要显示信息的自定义单元格。我制作了 2 个新电池,但它只返回一个电池。最上面的单元格标识符是 "Cell",最下面的是 "quantityCell"。我希望产品说明位于顶部单元格,数量位于底部单元格。我现在得到的输出只是显示数量单元格,而不是产品单元格。如果我删除数量单元格,将显示产品单元格。我应该怎么做呢?
谢谢
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *quantityCellIdentifier = @"quantityCell";
UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
self.productName = [inventoryItem getProductTitle];
productTitle = self.productName;
cell.detailTextLabel.text = productTitle;
NSLog(@"productTitle %@", self.productName);
cell.textLabel.text = @"Product Title";
self.productQuantity = [inventoryItem getQuantity];
quantityCell.textLabel.text = @"Quantity";
quantityCell.detailTextLabel.text = self.productQuantity;
/*
// Configure the cell...
// cell.textLabel.text = productTitle;
cell.textLabel.text = @"Product Name";
cell.detailTextLabel.text = productTitle;
*/
return cell;return quantityCell;
}
问题
您不能 return 在一个方法上同时使用 2 个参数。所以你在哪里说:
return cell;return quantityCell;
只有单元格正在 returned,该方法在第一个 return.
结束
解决方案
你需要制定逻辑,一次是 returns topCell,另一次是 bottomCell
所以在你的情况下你只需要 indexPath.row modulus 2,它可以取任何值并且 returns 在偶数上只有 0 并且1 奇数。
所以你的代码应该是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"";
if (indexPath.row%2==0) {
//Get the top cell
CellIdentifier = @"Cell";
} else {
//Get bottom cell
CellIdentifier = @"quantityCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
if (indexPath.row%2==0) {
//Customize topCell
self.productName = [inventoryItem getProductTitle];
productTitle = self.productName;
cell.detailTextLabel.text = productTitle;
NSLog(@"productTitle %@", self.productName);
cell.textLabel.text = @"Product Title";
} else {
//Customize bottom cell
self.productQuantity = [inventoryItem getQuantity];
cell.textLabel.text = @"Quantity";
cell.detailTextLabel.text = self.productQuantity;
}
return cell;
}
return cell;
return quantityCell;
你在函数中写的上面2行!在 return 语句之后,该方法停止执行更多行。
你可以这样做:
self.productName = [inventoryItem getProductTitle];
self.productQuantity = [inventoryItem getQuantity];
productTitle = self.productName;
if(indexPath.row == 0) {
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.detailTextLabel.text = productTitle;
cell.textLabel.text = @"Product Title";
return cell;
}
else {
NSString *quantityCellIdentifier = @"quantityCell";
UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
quantityCell.textLabel.text = @"Quantity";
quantityCell.detailTextLabel.text = self.productQuantity;
return quantityCell;
}
希望这对您有所帮助..:)
谢谢大家,我最终使用了一本字典,其中 returns 单元格的所有键和值。
NSDictionary *inventoryDictionary = @{
@"Quantity" : productQuantity,
@"Price" : productPriceValue,
@"Product Title" : productTitle
};
NSLog(@"ProductStrings: %@", inventoryDictionary);
NSString *keys = [inventoryDictionary allKeys][indexPath.row];
NSString *providerNameString = keys;
cell.textLabel.text = providerNameString;
NSString *Values = [inventoryDictionary allValues][indexPath.row];
NSString *providerNameValue = Values;
cell.detailTextLabel.text = providerNameValue;
return cell;
我有几个要显示信息的自定义单元格。我制作了 2 个新电池,但它只返回一个电池。最上面的单元格标识符是 "Cell",最下面的是 "quantityCell"。我希望产品说明位于顶部单元格,数量位于底部单元格。我现在得到的输出只是显示数量单元格,而不是产品单元格。如果我删除数量单元格,将显示产品单元格。我应该怎么做呢? 谢谢
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *quantityCellIdentifier = @"quantityCell";
UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
self.productName = [inventoryItem getProductTitle];
productTitle = self.productName;
cell.detailTextLabel.text = productTitle;
NSLog(@"productTitle %@", self.productName);
cell.textLabel.text = @"Product Title";
self.productQuantity = [inventoryItem getQuantity];
quantityCell.textLabel.text = @"Quantity";
quantityCell.detailTextLabel.text = self.productQuantity;
/*
// Configure the cell...
// cell.textLabel.text = productTitle;
cell.textLabel.text = @"Product Name";
cell.detailTextLabel.text = productTitle;
*/
return cell;return quantityCell;
}
问题
您不能 return 在一个方法上同时使用 2 个参数。所以你在哪里说:
return cell;return quantityCell;
只有单元格正在 returned,该方法在第一个 return.
结束解决方案
你需要制定逻辑,一次是 returns topCell,另一次是 bottomCell
所以在你的情况下你只需要 indexPath.row modulus 2,它可以取任何值并且 returns 在偶数上只有 0 并且1 奇数。
所以你的代码应该是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"";
if (indexPath.row%2==0) {
//Get the top cell
CellIdentifier = @"Cell";
} else {
//Get bottom cell
CellIdentifier = @"quantityCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
if (indexPath.row%2==0) {
//Customize topCell
self.productName = [inventoryItem getProductTitle];
productTitle = self.productName;
cell.detailTextLabel.text = productTitle;
NSLog(@"productTitle %@", self.productName);
cell.textLabel.text = @"Product Title";
} else {
//Customize bottom cell
self.productQuantity = [inventoryItem getQuantity];
cell.textLabel.text = @"Quantity";
cell.detailTextLabel.text = self.productQuantity;
}
return cell;
}
return cell;
return quantityCell;
你在函数中写的上面2行!在 return 语句之后,该方法停止执行更多行。
你可以这样做:
self.productName = [inventoryItem getProductTitle];
self.productQuantity = [inventoryItem getQuantity];
productTitle = self.productName;
if(indexPath.row == 0) {
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.detailTextLabel.text = productTitle;
cell.textLabel.text = @"Product Title";
return cell;
}
else {
NSString *quantityCellIdentifier = @"quantityCell";
UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
quantityCell.textLabel.text = @"Quantity";
quantityCell.detailTextLabel.text = self.productQuantity;
return quantityCell;
}
希望这对您有所帮助..:)
谢谢大家,我最终使用了一本字典,其中 returns 单元格的所有键和值。
NSDictionary *inventoryDictionary = @{
@"Quantity" : productQuantity,
@"Price" : productPriceValue,
@"Product Title" : productTitle
};
NSLog(@"ProductStrings: %@", inventoryDictionary);
NSString *keys = [inventoryDictionary allKeys][indexPath.row];
NSString *providerNameString = keys;
cell.textLabel.text = providerNameString;
NSString *Values = [inventoryDictionary allValues][indexPath.row];
NSString *providerNameValue = Values;
cell.detailTextLabel.text = providerNameValue;
return cell;