0
我有一個包含UITextField的自定義UITableViewCell原型。在最後一節填寫UITextField時,我會自動添加新的單元格。哪裏有更多的部分不適合在一個屏幕上,我添加另一個部分,它會充滿第一部分的值,並且第一部分輸入被清空!當視圖滾動屏幕時,UITextField內容會丟失
截圖:
相關代碼:
@implementation TMNewTripPeopleViewController
@synthesize sectionsCount = _sectionsCount;
@synthesize firstCellShowed = _firstCellShowed;
- (int) sectionsCount
{
if (_sectionsCount == 0) {
_sectionsCount = 1;
}
return _sectionsCount;
}
- (IBAction)inputChanged:(id)sender {
UITextField* input = (UITextField*) sender;
NSIndexPath* indexPathName = [NSIndexPath indexPathForRow:0 inSection:input.tag - 1];
UITableViewCell* cellName = [self.tableView cellForRowAtIndexPath:indexPathName];
if (input == [cellName viewWithTag:input.tag]) {
// last name input - add next section?
if (input.tag == self.sectionsCount) {
if (input.text.length > 0) {
self.sectionsCount++;
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:self.sectionsCount - 1] withRowAnimation:UITableViewRowAnimationTop];
}
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.sectionsCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"PersonName";
if (indexPath.row % 2 == 1) {
CellIdentifier = @"PersonEmail";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField* input = (UITextField*) [cell viewWithTag:indexPath.section + 1];
if (indexPath.row == 0 && indexPath.section == 0 && !self.firstCellShowed) {
[input becomeFirstResponder];
self.firstCellShowed = YES;
}
[cell viewWithTag:1].tag = indexPath.section + 1;
return cell;
}
@end
所以我應該保存這些值在一個模型關聯的單元格行+部分每次他們改變? – 2012-07-08 13:00:56
是的,確實如此。你可能想結賬。 http://stackoverflow.com/questions/4561424/uitableview-tutorial – logancautrell 2012-07-08 23:51:39