我试图在我的 cellForRowAtIndexPath
方法中向单元格添加一个按钮。这行得通,但我似乎在每次更新单元格时都添加了一个按钮(发现这一点是因为我更新了错误的屏幕位置,并且我有 2 个既可以工作又可以点击的按钮)。
分配和释放添加到单元格的 UIButton
的正确时间是什么时候?当然,我不能在 cellForRowAtIndexPath
末尾释放按钮 - 这会破坏按钮对象,不是吗?到目前为止,我发现所有将按钮添加到单元格的片段似乎都在 cellForRowAtIndexPath
中保持实例化新的 UIButton
对象,这让我有些困惑..
最佳答案
一些事情:
按钮出现在其他地方,因为您对不同类型的单元格使用相同的单元格标识符。每种类型的细胞都应该有自己的标识符。
就按钮消失而言,我不知道为什么会因为
reloadRowsAtIndexPaths
而发生这种情况,但是如果您将按钮移到其他 subview 的前面,您就赢了失去它。您可能想要
自动释放
您的 UITextField。 (同样适用于titleForHeaderInSection
中的 NSString。)
无论如何,cellForRowAtIndexPath
变成这样:
#define kTextFieldTag 100
#define kButtonFieldTag 101
//-----------------------------------------------------------------------------------------
// cellForRowAtIndexPath
//-----------------------------------------------------------------------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier;
UITableViewCell *cell;
// Configure the cell...
switch( [indexPath section] )
{
case WORKSHEET_SECTION_CLIENT:
cellIdentifier = @"CellClient";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = clientName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case WORKSHEET_SECTION_PERIOD:
{
UIButton *myButton1;
cellIdentifier = @"CellPeriod";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
// Don't need UIButton alloc because method below does it for us..
myButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton1.tag = kButtonFieldTag;
CGRect cellFrame = [self.tableView rectForRowAtIndexPath:indexPath];
int nButtonWidth = 80;
int nButtonHeight = 30;
int nActualCellWidth = cellFrame.size.width - 40;
int nButtonDeltaX = ( nActualCellWidth - nButtonWidth ) / 2;
int nButtonDeltaY = ( cellFrame.size.height - nButtonHeight ) / 2;
myButton1.frame = CGRectMake( nButtonDeltaX, nButtonDeltaY, nButtonWidth, nButtonHeight );
[myButton1 setTitle:@"OK" forState:UIControlStateNormal];
// PDS: Add delaget for stopwatch clicking..
[myButton1 addTarget:self action:@selector(buttonClickedStopWatch:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:myButton1];
}
else
{
myButton1 = (UIButton *)[[cell contentView] viewWithTag:kButtonFieldTag];
}
cell.textLabel.text = timeElapsed;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[[cell contentView] bringSubviewToFront:myButton1];
break;
}
case 2:
cellIdentifier = @"Cell2";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = @"Amount $130";
break;
case 3:
{
cellIdentifier = @"Cell3";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UITextField *myTextField;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
myTextField = [[[UITextField alloc] initWithFrame:CGRectMake(20,10,125,25)] autorelease];
[myTextField setTag:kTextFieldTag];
myTextField.adjustsFontSizeToFitWidth = NO;
myTextField.backgroundColor = [UIColor clearColor];
myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
myTextField.textAlignment = UITextAlignmentLeft;
myTextField.keyboardType = UIKeyboardTypeDefault;
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.clearButtonMode = UITextFieldViewModeNever;
// myTextField.delegate = self;
myTextField.font = [UIFont systemFontOfSize:14];
[cell addSubview:myTextField];
}
else
{
myTextField = (UITextField *)[[cell contentView] viewWithTag:kTextFieldTag];
}
// note, theoretically, you could blow away anything that the user typed here
[myTextField setPlaceholder:@"Type Data Here"];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.numberOfLines = 0;
break;
}
default:
cellIdentifier = @"CellDummy";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = @"Dummy";
break;
}
return cell;
}
就个人而言,我可能会将其拆分为单独的方法,但这取决于您。
关于ios - 什么时候释放 UITableView 单元格中的 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11271590/