如何在同一XIB文件中将UITableViewCell连接到UITableView?

3
我有一个带有UITableViewUITableViewCell的XIB文件。

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

编辑

enter image description here

编辑

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

谢谢大家。


你的意思是想要添加自定义单元格吗? - Dharmbir Singh
此单元格应仅显示一个文本标签,因此我不想创建新类...只需使用基本单元格即可。 - Romowski
请在 cellForRowAtIndexPath 中展示您的代码。我认为您犯了一个错误。 - Mani
你展示不同的xib文件,但是你想将它们连接到一个文件的所有者上吗?这是否可能? - Mani
@Romowski,如果你只想要标题,那么就不需要自定义单元格,你可以使用我的答案。 - Dharmbir Singh
8个回答

2
在您的customcell.xib文件中,您需要进行以下操作: enter image description here 在您的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中。
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? - user3818576

1

如果您想在表格视图中仅显示一个标签,请尝试使用此选项。无需自定义单元格。

- (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"];
}

0
通过继承UITableViewCell(比如说TableCell),创建一个新的类,然后在cellForRowAtIndexPath方法中使用以下代码片段。
static NSString *CellIdentifier = @"CellIdentifier";
TableCell *cell = (TableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

这个单元格应该只显示一个文本标签,所以我不想创建新的类... 只需要使用基本单元格就足够了... 谢谢。 - Romowski
@Romowski 那么,您如何使用您展示的自定义单元格视图(customTableViewCell nib)? - Mani

0

CellForRowAtIndexpath和其他方法都是委托方法,它们不需要被“连接”。

包括

@interface YOURTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>

将表格连接到数据源和委托文件所有者


0
[[NSBundle mainBundle]loadNibNamed:@"TableCellXibName" owner:self options:nil];

cell和tableView在同一个XIB中。 - Romowski
1
为什么要这么做呢?你可以新建一个视图并将其添加到UITableViewCell中,它只需要一分钟的解决方案。 - hitesh

0
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];

    }

0

你需要将代理和数据源设置到你的TableView中。

有两种方法可以实现:

1)如果你的视图控制器是UITableViewController,只需在xib中找到TableViewDatasource和delegate,并将其拖到文件所有者中即可。

2)否则,如果你使用的是UIViewController...

在你的.h文件中:

@interface YourViewController<UITableviewDelegate,UITableviewDatasource>

在viewDidLoad中。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接