Iphone 在UITableView中添加多个自定义单元格
虽然这是问得最多的问题之一,但我找不到一个全面的答案。我需要在UITableView中设置自定义单元格。有些包含标签或文本字段,有些包含图像和按钮。我已经为每种类型的细胞做了单独的分类。我正在使用具有多个部分的GroupStyle表。现在,我正在cellForIndexPath中添加单元格,并为节中的行使用开关大小写,为节中的行使用if-else:Iphone 在UITableView中添加多个自定义单元格,iphone,uitableview,custom-cell,Iphone,Uitableview,Custom Cell,虽然这是问得最多的问题之一,但我找不到一个全面的答案。我需要在UITableView中设置自定义单元格。有些包含标签或文本字段,有些包含图像和按钮。我已经为每种类型的细胞做了单独的分类。我正在使用具有多个部分的GroupStyle表。现在,我正在cellForIndexPath中添加单元格,并为节中的行使用开关大小写,为节中的行使用if-else: id cell; switch(indexPath.section) { case 0: if(indexPath.r
id cell;
switch(indexPath.section) {
case 0:
if(indexPath.row==0) {
CellA *cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
else if(indexPath.row==1) {
CellB *cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
break;
case 1:
if(indexPath.row==0) {
CellC *cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
break;
default:
break;
}
return cell;
我还必须在末尾返回cell,因为由于代码块中的单元格定义,单元格变得不可识别。为了解决这个问题,我声明了一个id在上面的单元格,但我知道这不是正确的方法。如何解决多类型单元格的声明和访问问题
此时有4-5行适合一个屏幕,不需要滚动。所以,我不是在重复使用细胞。但在编辑时会有更多的行挤进来。在另一个表中,有更多的行可以滚动屏幕。这意味着我必须重复使用细胞。所以,我问题的第二部分是;如何重用多个自定义单元格?要回答第一个问题,您最好返回
nil
,因为您没有好的返回值。如果遇到这种情况,将抛出异常;现在,它可能会在框架代码中的某个地方为您提供EXC_BAD_访问权限
要回答第二个问题,每种类型的单元格都应该有一个唯一的reuseIdentifier。例如,所有的CellA都可以有一个重用标识符@“CellA”。然后,您将完全像在所有单元格都相同的情况下一样重用它们:当您需要CellA调用时[tableView dequeueReusableCellWithIdentifier:@“CellA”]
,当您需要CellB调用时[tableView dequeueReusableCellWithIdentifier:@“CellB”]
,等等。比如说,
case 0:
if (indexPath.row == 0) {
CellA *cell = [tableView dequeueReusableCellWithIdentifier:@"CellA"];
if (!cell) {
cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellA"] autorelease];
}
// configure cell
return cell;
}
else if (indexPath.row == 1) {
CellB *cell = [tableView dequeueReusableCellWithIdentifier:@"CellB"];
if (!cell) {
cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellB"] autorelease];
}
// configure cell
return cell;
}
break;
case 1:
if (indexPath.row == 0) {
CellC *cell = [tableView dequeueReusableCellWithIdentifier:@"CellC"];
if (!cell) {
cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellC"] autorelease];
}
// configure cell
return cell;
}
break;
将UITableView添加到UIView。添加自定义单元格,关联自定义单元格类并实现委托-UITableviewDelegate和UITableViewDataSource 案例1:tableview上的两个自定义单元格 func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
var cell: CustomCell!
if indexPath.row == 0{
cell = tableView.dequeueReusableCellWithIdentifier("Cell1ID", forIndexPath: indexPath) as CustomCell
//set cell2
}
if indexPath.row >= 1{
cell = tableView.dequeueReusableCellWithIdentifier("Cell2ID", forIndexPath: indexPath) as CustomCell
let cons = aArray[indexPath.row - 1]
// set cell2
}
return cell
}
案例2:自定义单元格的交替显示(即使用uisegmentcontrol)
情况3:备用自定义单元格(即奇偶)
感谢您的回复。为了安全起见,我正在为默认情况定义一个单元格,希望剩余的任何情况都可以默认处理,并且不会影响最后一个返回语句。您不需要切换情况
var CellIdentifier: String = "Cell1ID"
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (CellIdentifier == "Cell1ID")
{
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! FirstTableViewCell
//additional code
return cell
}
else {
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! SecondTableViewReportsCell
//Additional code
return cell
}
}
var CellIdentifier: String = "Cell1ID"
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: CustomCell!
if (indexPath.row % 2 == 0) {
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! FirstTableViewCell
}
else
{
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! SecondTableViewCell
}
return cell
}