以编程方式将UITableViewCell添加到UITableView

3

我已经通过编程方式创建了一个自定义的UITableViewCell,并希望将它添加到在IB中创建的UITableViewController的UITableView中。我该怎么做?

2个回答

2
尝试这样做 -
在cellForRowAtIndexPath方法中添加自定义单元格,就像这样 -
static NSString *CellIdentifier = @"DashboardCell";

DashboardTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[DashboardTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

如果单元格包含图像、文本等内容,请按如下方式添加 -
cell.image.image = [UIImage imageNamed:[apdelobj.array1_25 objectAtIndex:myInt]];

cell.titleLabel.text = dashboardList.mistohMessage;

0

UITableViewDataSource 的代理方法 -(UITableViewCell*)tableView:(UITableView*)t cellForRowAtIndexPath:(NSIndexPath*)indexPath 就是为了这个而存在的,无论 cell 是以编程方式还是在 IB 中创建的,只需从此方法返回您创建的 cell 即可。不要忘记使用您的数据填充创建的 cell。

因此,您必须提供 UITableViewDataSource 方法。除上述方法外,此代理还有一个必需的方法:-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section。其他所有内容都不是必需的,但通常很有用。请参阅 UITableViewDataSource 协议reference


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