Ios 具有多个原型单元的TableView
我有一个简单的问题,关于带有3种不同类型的原型单元的表视图。前两个只发生一次,而第三个发生4次。现在我困惑的是如何在我的cellforRowatindexpath中指定用于哪一行的单元格原型。所以,我想对第0行使用prototype 1,对第1行使用prototype 2,对第3、4、5和6行使用prototype 3。最好的方法是什么?我是否给每个原型一个标识符,然后使用dequeueReusableCellWithIdentifier:CellIdentifier? 你能提供一些示例代码吗 编辑: 仍然不起作用。这是我目前掌握的密码。(对于switch station,我只有一个case,因为我只想测试并查看单元格是否在第一行生成,但当前表视图为空)Ios 具有多个原型单元的TableView,ios,objective-c,uitableview,uistoryboard,Ios,Objective C,Uitableview,Uistoryboard,我有一个简单的问题,关于带有3种不同类型的原型单元的表视图。前两个只发生一次,而第三个发生4次。现在我困惑的是如何在我的cellforRowatindexpath中指定用于哪一行的单元格原型。所以,我想对第0行使用prototype 1,对第1行使用prototype 2,对第3、4、5和6行使用prototype 3。最好的方法是什么?我是否给每个原型一个标识符,然后使用dequeueReusableCellWithIdentifier:CellIdentifier? 你能提供一些示例代码吗
Acell是我创建的单元原型的标识符。I如果使用三个原型,则使用三个标识符。因为只有一个标识符会导致问题。你会得到错误的结果。所以像这样编码
if(indexPath.row==0){
// Create first cell
}
if(indexPath.row==1){
// Create second cell
}
else{
// Create all others
}
您也可以在这里使用switch case以获得最佳性能。在这里,我编写了如下代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell.tag == 0)
{
return array1.count;
}
else(cell.tag == 1)
{
return array2.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier;
NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row];
if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"])
{
cellIdentifier = @"cell";
}
else if ([membershipType isEqualToString:@"platinum"])
{
cellIdentifier = @"premiumCustomCell";
cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row];
}
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row];
}
#pragma mark == Tableview Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger nRows = 0;
switch (section) {
case 0:
nRows = shipData.count;
break;
case 1:
nRows = dataArray1.count;
break;
default:
break;
}
return nRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cellIdentifier1";
NSString *cellIdentifier1 = @"cellIdentifier2";
SingleShippingDetailsCell *cell;
switch (indexPath.section) {
case 0:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Load data in this prototype cell
break;
case 1:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
//Load data in this prototype cell
break;
default:
break;
}
return cell;
}
我不理解你的问题。“我是否给每个原型一个标识符,然后使用dequeueReusableCellWithIdentifier:CellIdentifier?”是的,你知道。您几乎已经回答了自己的问题。但我如何选择哪个原型应用于哪一行?就像最好的编码器在他的回答中所显示的那样。@rdelmar请参阅我的问题中的编辑在创建单元格时,您可以遵循相同的方法,然后再使用单个标识符。但在这里,您将在deque期间使用三个标识符。如何处理节中的行数。如果您有2个原型单元格,并且没有根据数组计数显示的行数?您只需标记自定义原型单元格并检查每个单元格的数组计数,即可返回“array.count”
#pragma mark == Tableview Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger nRows = 0;
switch (section) {
case 0:
nRows = shipData.count;
break;
case 1:
nRows = dataArray1.count;
break;
default:
break;
}
return nRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cellIdentifier1";
NSString *cellIdentifier1 = @"cellIdentifier2";
SingleShippingDetailsCell *cell;
switch (indexPath.section) {
case 0:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Load data in this prototype cell
break;
case 1:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
//Load data in this prototype cell
break;
default:
break;
}
return cell;
}