多个自定义行UITableView? [英] Multiple Custom Rows UITableView?
问题描述
我一直在搜索很多,但没有找到任何与多个自定义行有关的内容,我需要为我的应用程序创建一个设置tableView,其中我需要从xib文件加载行,如:
I've been searching a lot but didn't find anything useful related to multiple custom rows, I need to create a settings tableView for my app,in which I need to load the rows from xib files,like:
ROW 1 = >> XIB 1.
ROW 2 = >> XIB 2.
ROW 3 = >> XIB 3 。
ROW 4 = >> XIB 4.
ROW 1 =>> XIB 1.
ROW 2 =>> XIB 2.
ROW 3 =>> XIB 3.
ROW 4 =>> XIB 4.
我现在的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=nil;
//We use CellType1 xib for certain rows
if(indexPath.row==0){
static NSString *CellIdentifier = @"ACell";
cell =(ACell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"ACell" owner:self options:nil];
cell = (ACell *)[nib objectAtIndex:0];
}
//Custom cell with whatever
//[cell.customLabelA setText:@"myText"]
}
//We use CellType2 xib for other rows
else{
static NSString *CellIdentifier = @"BCell";
cell =(BCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"BCell" owner:self options:nil];
cell = (BCell *)[nib objectAtIndex:0];
}
//Custom cell with whatever
//[cell.customLabelB setText:@"myText"]
}
return cell;
}
推荐答案
首先创建一些自定义UITableViewCell类(.h和.m),就像你有xib文件一样多:
所以你可以使用CellType1和CellType2作为例子。
CellType1.h看起来像
First you create some custom UITableViewCell classes (.h and .m), as many as you have xib files:
So you could have CellType1 and CellType2 for example.
CellType1.h would look something like
#import <UIKit/UIKit.h>
@interface CellType1 : UITableViewCell
@property(nonatomic,strong) IBOutlet UILabel *customLabel;
@end
然后你创建xib文件,你可以使用默认值视图类型,但是,只需删除自动创建的视图,将其替换为UITableViewCell,并将类更改为CellType1。对CellType2做同样的事情。
Then you create the xib files, you can use default view type, but then, just remove the view that is automatically created, replace that by a UITableViewCell, and change the class to CellType1. Do the same for CellType2.
然后在你的tableViewController中,像这样写一下cellForRow:
Then in your tableViewController, write cellForRow like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=nil;
//We use CellType1 xib for certain rows
if(indexPath.row==<whatever you want>){
static NSString *CellIdentifier = @"CellType1";
cell =(CellType1*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType1" owner:self options:nil];
cell = (CellType1 *)[nib objectAtIndex:0];
}
//Custom cell with whatever
[cell.customLabel setText:@"myText"]
}
//We use CellType2 xib for other rows
else{
static NSString *CellIdentifier = @"CellType2";
cell =(CellType2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType2" owner:self options:nil];
cell = (CellType2 *)[nib objectAtIndex:0];
}
//Custom cell with whatever
[cell.customLabel setText:@"myText"]
}
return cell;
}
这篇关于多个自定义行UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!