【问题标题】:How can i use custom UITableViewCell and UITableView in same xib我如何在同一个 xib 中使用自定义 UITableViewCell 和 UITableView
【发布时间】:2013-03-01 00:03:47
【问题描述】:

我想在同一个 xib 中使用自定义 UITableviewCell 和 UITableView 而不创建 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];
  }

}

但不工作?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    将 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;
    }
    

    【讨论】:

    • 谢谢,但 UITableViewCell 和 UItableView 在一个 xib 中
    • 您可以将 tableviewcell 移动到单独的 xib。
    • 请注意,如果您想要自定义 XIB AND 自定义类,请在此处使用 GauravDS 的答案stackoverflow.com/questions/11025572/…
    【解决方案2】:

    是的,您可以按照以下代码进行操作

    在 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];
    }
    

    希望对你有帮助!!!

    【讨论】:

    • 感谢重播,但我想使用可视化 UiTableCell 并且不在 xib 中为其创建类,在故事板中很容易做到这一点,但我不知道如何在 xib 文件中做到这一点跨度>
    • @ArashZeinoddini 实际上此代码仅适用于 xib .. 你能简要介绍一下你的问题吗?我不知道你的问题是什么。
    • 你知道,我想创建一个自定义 UITableViewCell,好吗?,正常的流程是创建一个 UITableViewCell 及其类,例如本教程:altinkonline.nl/tutorials/xcode/uitableview/uitableviewcell,好吗?,但我真的不想制作自定义 UITableViewCell 类 (.h/.m),只想制作一个与我创建 UITableView 相同的 UITableViewCell,好的,当然我为使用这个创建的 UITableViewCell 设置了一个标识符。
    【解决方案3】:

    看..你可以这样做。在这里,您使用 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];
      }
    

    与代码一起,在 Class 的 XIB 中拖放两个 UITableViewCell,它们应该在视图之外,并使用 Files Owner 添加这些单元格。 它将完美地工作。这里不需要创建一个单独的自定义 UITableViewCell 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      相关资源
      最近更新 更多