iPhone:在同一tableView中使用UITableViewCellStyleValue1和UITableViewCellStyleValue2
在同一个tableView中使用UITableViewCellStyleValue1和UITableViewCellStyleValue2的最佳方式是什么?我不确定这样做的最佳方式,因为我在转换之前使用了以下方式:iPhone:在同一tableView中使用UITableViewCellStyleValue1和UITableViewCellStyleValue2,iphone,cocoa-touch,uitableview,Iphone,Cocoa Touch,Uitableview,在同一个tableView中使用UITableViewCellStyleValue1和UITableViewCellStyleValue2的最佳方式是什么?我不确定这样做的最佳方式,因为我在转换之前使用了以下方式: // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Username";
break;
case 1:
cell.textLabel.text = @"Password";
break;
}//end switch
} else if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Create Account";
break;
}//end switch
}
return cell;
}//end
如何在我的“创建帐户”单元格中使用UITableViewCellStyleValue2?为什么不在交换机中移动initWithStyle?如果您试图节省一些空间,并且需要Value2与Value1进行比较,那么您可以在切换之前保留initWithStyle,然后在切换中为Value2样式的单元格再次创建它。在调用initWithStyle之前确定单元格样式和标识符 您需要为每个样式使用不同的单元格重用标识符,因为如果样式值为1的单元格被取消排队,则您不希望将其用于需要样式值2的行
static NSString *CellIdentifierValue1 = @"CellIdentifierValue1";
static NSString *CellIdentifierValue2 = @"CellIdentifierValue2";
//default settings...
NSString *reuseIdentifier = CellIdentifierValue1;
UITableViewCellStyle cellStyle = UITableViewCellStyleValue1;
if ((indexPath.section == 1) && (indexPath.row == 0))
{
//special settings for CreateAccount...
reuseIdentifier = CellIdentifierValue2;
cellStyle = UITableViewCellStyleValue2;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:cellStyle reuseIdentifier:reuseIdentifier] autorelease];
}
//rest of existing code...
你是说我可以做另一个initWithStyle,我希望那个单元格是style2?当然。而且,由于您在第一条语句中使用了autorelease,所以您甚至不需要担心内存问题。