How can i use custom UITableViewCell and UITableView in same xib

后端 未结 3 690
既然无缘
既然无缘 2020-12-21 01:58

I want to use a custom UITableviewCell with UITableView in same xib without creating a UITableViewCell class?

As you can see bellow i set the identifier for UITableV

3条回答
  •  余生分开走
    2020-12-21 02:45

    Yes you can do as following code

    Under ViewDidLoad or 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];
    }
    

    Hope this Helps !!!

提交回复
热议问题