いくつかのこと:
異なる種類のセルに同じセル識別子を使用しているため、ボタンが別の場所に表示されています。セルの各タイプは、独自の識別子を取得する必要があります。
ボタンが消えるという点では、結果としてなぜそうなったのかはわかりませんが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;
}
個人的には、これを別々の方法に分割するかもしれませんが、それはあなた次第です。