我如何在相同的xib中使用自定义UITableViewCell和UITableView
我想在同一个xib中使用带有UITableView的自定义UITableviewCell而不创建UITableViewCell类?
正如您所见,我为UITableViewCell设置了标识符,并使用它如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } }
但不工作?
将UITableViewCell * customCell属性添加到视图控制器(例如,您的文件ShowUsers.h)…
@property (nonatomic, strong) IBOutlet UITableViewCell *customCell;
并将其连接到xib中的自定义单元格。 然后在cellForRow中使用以下代码(例如,您的文件ShowUsers.m):
if (cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"NibName" owner:self options:nil]; cell = self.customCell; self.customCell = nil; }
是的,您可以执行以下代码
在ViewDidLoad或ViewWillAppear下
label1Array=[NSMutableArray arrayWithObjects:@"A",@"B",nil]; label2Array=[NSMutableArray arrayWithObjects:@"C",@"D",nil]; label3Array=[NSMutableArray arrayWithObjects:@"E",@"F",nil];
的UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"aCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UILabel *label1=[[UILabel alloc]init]; label1.frame=CGRectMake(0,0,40,40); label1.text=[label1Array objectAtIndex:indexPath.row]; ................ [cell.contentView addSubView: label1]; UILabel *label2=[[UILabel alloc]init]; label2.frame=CGRectMake(50,0,40,40); label2.text=[label2Array objectAtIndex:indexPath.row]; [cell.contentView addSubView: label2]; UILabel *label3=[[UILabel alloc]init]; label3.frame=CGRectMake(100,0,40,40); label3.text=[label3Array objectAtIndex:indexPath.row]; [cell.contentView addSubView: label3]; }
希望这可以帮助 !!!
见..你可以这样做。 在这里,您将使用XIB在一个TableView中添加两个UITableViewCell。
在Class.h文件中: –
为单元格数量声明一个可变数组。
{ NSMutableArray * sectionRows; } @property (nonatomic,retain) IBOutlet UITableViewCell *addTvc1; @property (nonatomic,retain) IBOutlet UITableViewCell *addTvc2;
在Class.m中: –
@synthesize addTvc1, addTvc2; - (void)viewDidLoad { [super viewDidLoad]; sectionRows = [[NSMutableArray alloc] init]; [sectionRows addObject:[[NSMutableArray alloc] initWithObjects:addTvc1, nil]]; [sectionRows addObject:[[NSMutableArray alloc] initWithObjects:addTvc2, nil]]; }
在UITableViewDelegate方法中 –
添加这个 –
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [sectionRows count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { return [[sectionRows objectAtIndex:section] count]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { return [[sectionRows objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; }
与代码一起,In Class的XIB删除并拖动两个应该在视图外部的UITableViewCell,并使用Files Owner添加这些单元格。 它会很完美。 这里不需要创建一个单独的自定义UITableViewCell类。