有几件事:
按钮出现在别处是因为您对不同类型的单元格使用相同的单元格标识符。每种类型的单元格都应该有自己的标识符。
在按钮消失方面,我不知道为什么会发生这种情况reloadRowsAtIndexPaths
,但是如果您将按钮移动到其他子视图的前面,您将不会丢失它。
你可能想要autorelease
你的 UITextField。(与 NSString 中的相同titleForHeaderInSection
。)
无论如何,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;
}
就个人而言,我可能会将其拆分为单独的方法,但这取决于您。