Mat*_*eus 2 iphone uitableview custom-cell ios
我一直在搜索很多,但没有发现任何与多个自定义行有关的内容,我需要为我的应用程序创建一个设置tableView,其中我需要从xib文件加载行,如:
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;
}
Run Code Online (Sandbox Code Playgroud)
首先,您创建一些自定义UITableViewCell类(.h和.m),就像您拥有xib文件一样多:
例如,您可以使用CellType1和CellType2.
CellType1.h看起来像
#import <UIKit/UIKit.h>
@interface CellType1 : UITableViewCell
@property(nonatomic,strong) IBOutlet UILabel *customLabel;
@end
Run Code Online (Sandbox Code Playgroud)
然后创建xib文件,可以使用默认视图类型,但是,只需删除自动创建的视图,将其替换为UITableViewCell,然后将类更改为CellType1.对CellType2执行相同的操作.
然后在你的tableViewController中,像这样写下cellForRow:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5450 次 |
| 最近记录: |