我已经通过编程方式创建了一个自定义的UITableViewCell,并希望将它添加到在IB中创建的UITableViewController的UITableView中。我该怎么做?
我已经通过编程方式创建了一个自定义的UITableViewCell,并希望将它添加到在IB中创建的UITableViewController的UITableView中。我该怎么做?
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;
UITableViewDataSource
的代理方法 -(UITableViewCell*)tableView:(UITableView*)t cellForRowAtIndexPath:(NSIndexPath*)indexPath
就是为了这个而存在的,无论 cell 是以编程方式还是在 IB 中创建的,只需从此方法返回您创建的 cell 即可。不要忘记使用您的数据填充创建的 cell。
因此,您必须提供 UITableViewDataSource
方法。除上述方法外,此代理还有一个必需的方法:-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
。其他所有内容都不是必需的,但通常很有用。请参阅 UITableViewDataSource
协议reference。