Ios 如何显示在同一nib文件中定义的自定义UITableViewCell
我在与UITableView相同的nib文件中设计了一个自定义UITableViewCell。然后,我将使用以下代码显示它:Ios 如何显示在同一nib文件中定义的自定义UITableViewCell,ios,uitableview,Ios,Uitableview,我在与UITableView相同的nib文件中设计了一个自定义UITableViewCell。然后,我将使用以下代码显示它: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"SearchViewCell"; UITableViewCell *cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SearchViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSInteger section = [indexPath section];
switch (section) {
case 0: // First cell in section 1
return self.priceRangeCell;
break;
default:
// Do something else here if a cell other than 1,2,3 or 4 is requested
return cell;
break;
}
我的问题是,我对整个dequeueReusableCellWithIdentifier业务感到困惑。我是否需要以下部分??:
static NSString *CellIdentifier = @"SearchViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
你能看到我的代码还有其他问题吗
谢谢您不一定要使用您提到的代码,但不这样做是不好的。如果不使用If子句,则每次滚动表时都会创建新的单元格,而不是重用旧的单元格,这对内存使用不利
其次,您的代码中缺少了一些东西——您没有用任何东西填充您的单元格。您返回一个单元格,但未向其中添加任何内容。我的单元格是在interface builder中创建的。@jini,是的,但这不会将任何内容放入单元格中。您需要有一个模型(通常是一个数组),从中您可以根据填充的行获取数据(除非该单元格中只有静态数据,否则我想您可以将其填充到IB中)。是的,现在它是静态的。但是是的,我稍后会修改它。再次感谢