【问题标题】:How to connect UITableViewCell to UITableView from the same XIB?如何从同一个 XIB 将 UITableViewCell 连接到 UITableView?
【发布时间】:2013-05-08 09:31:49
【问题描述】:

我有 UITableViewUITableViewCell 的 XIB。

我应该如何将此单元格连接到我的 cellForRowAtIndexPath 中的 tableView?

已编辑

已编辑

我只是想在此单元格上方的 tableView 中使用屏幕中的单元格。我不想创建类,因为我只需要显示一个标签

谢谢大家

【问题讨论】:

  • 你的意思是你想添加自定义单元格?
  • 这个单元格应该只显示一个文本标签,所以我不想创建新类...足以使用基本单元格...
  • 请在cellForRowAtIndexPath 中显示您的代码。我认为你做错了。
  • 您显示不同的 xib 文件,但您想连接到一个文件所有者?有可能吗?
  • @Romowski 亲爱的,如果您只想在这种情况下获得标题,则不需要自定义单元格,您可以使用我的答案。

标签: ios uitableview xib


【解决方案1】:

在你的 customcell.xib 中你需要这样做

在你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method

static NSString *CellIdentifier = @"ViewCustomCell";
        ViewCustomCell *objAlertCustomCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (objAlertCustomCell == nil)
        {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ViewCustomCell" owner:self options:nil];
            for(id currentObject in topLevelObjects)
            {
                if ([currentObject isKindOfClass:[ViewCustomCell class]])
                {
                    objAlertCustomCell = (ViewCustomCell *) currentObject;
                    break;
                }
            }
        }
        objAlertCustomCell.lblAlertName.text = [[self.aryAlerts objectAtIndex:indexPath.row]valueForKey:@"vAlertName"];
        objAlertCustomCell.lblDate.text = [[self.aryAlerts objectAtIndex:indexPath.row]valueForKey:@"vDate"];
        objAlertCustomCell.lblDescription.text = [[self.aryAlerts objectAtIndex:indexPath.row]valueForKey:@"vDescription"];
        return objAlertCustomCell;

【讨论】:

  • @monahar。如何将我的 xib 连接到 segue?
【解决方案2】:

如果您只想在表格视图中显示一个标签,请尝试使用此标签。而且不需要customcell。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

   UILabel *titleLbl;

    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];


        // -------------------- Label for Title Name ----------------------
        titleLbl = [[UILabel alloc]initWithFrame:CGRectMake(30, 7, 200, 13)];
        [titleLbl setTag:1];
        [cell addSubview:titleLbl];
    }
    else
    {
        titleLbl = (UILabel *)[cell viewWithTag:1];
     }

     [titleLbl setText:@"Title"];
}

【讨论】:

    【解决方案3】:

    通过继承 UITableViewCell(比如 TableCell)创建一个新类,并在 cellForRowAtIndexPath 中使用以下代码 sn-p

    static NSString *CellIdentifier = @"CellIdentifier";
    TableCell *cell = (TableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    【讨论】:

    • 这个单元格应该只显示一个文本标签,所以我不想创建新类...足以使用基本单元格...谢谢
    • @Romowski 那么你如何使用你展示的customTableViewCell nib?
    【解决方案4】:

    CellForRowAtIndexpath 等都是委托方法,不需要“连接”

    包括

    @interface YOURTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
    

    并将表连接到数据源并委托给文件所有者

    【讨论】:

      【解决方案5】:
      [[NSBundle mainBundle]loadNibNamed:@"TableCellXibName" owner:self options:nil];
      

      【讨论】:

      • cell & tableView 在一个XIB中
      • 你为什么这样做?您采用新视图并添加 UITableViewCell,只需一分钟的解决方案..
      【解决方案6】:
      static NSString *cellIdentifier = @"currencyCellIdentifier";
      
          // Try to retrieve from the table view a now-unused cell with the given identifier.
          CurrencyTableCell *cell = [tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];
          // If no cell is available, create a new one using the given identifier.
      
          if (cell == nil)
          {
              // Use the default cell style.
              cell = [[CurrencyTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
              NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CurrencyTableCell"
                                                           owner:self options:nil];
              cell = [nib objectAtIndex:0];
      
          }
      

      【讨论】:

        【解决方案7】:

        您必须为您的 tableviewView 设置委托和数据源。

        它可以通过两种方式制作。

        1)如果你的view controller是UITableViewController,在xib中找到TableViewDatasource和delegate,然后拖到你的文件所有者那里。

        2) 否则如果你使用 UIViewController..

        在您的 .h 文件中

        @interface YourViewController<UITableviewDelegate,UITableviewDatasource>
        

        在 viewDidload 中。

        self.tableView1.delegate = self;
        self.tableView1.datasource = self;
        

        【讨论】:

          【解决方案8】:
          猜你喜欢
          • 2013-03-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-23
          • 2012-03-28
          • 1970-01-01
          • 2020-12-09
          • 1970-01-01
          相关资源
          最近更新 更多