我正在嘗試實施基於UITableview
的應用程序。在我的tableView
他們是10節,每節有一行。 我要實現的每個部分都有不同類型的ContentView
(1-8個相同ContentView
第9個不同的ContentView
)。我爲此做了這個代碼。數據丟失在UITableViewCell上時UITableView滾動?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";
UITextField *textField;
UITextView *textView;
NSUInteger section=[indexPath section];
if(section == 9){
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
textView=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)];
[textView setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor
]];
[textView setTag:([indexPath section]+100)];
[cell.contentView addSubview:textView];
}else{
textView=(UITextView*)[cell.contentView viewWithTag:([indexPath section]+100)];
}
return cell;
}else {
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
textField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)];
[textField setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
[textField setTag:([indexPath section]+100)];
[cell.contentView addSubview:textField];
}else{
textField=(UITextField*)[cell.contentView viewWithTag:([indexPath section]+100)];
}
return cell;
}
return nil;
}
我的問題是:1。 類型後在UITextField
/UITextView
有些事情我在UITableView
滾動。那段時間UITableViewCell
(UITextField
/UITextView
)中的所有數據都丟失了,除了最後一個單元格數據。 2.如果我創建細胞
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
代替
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
的數據將重複。我怎麼能過來這個問題?
用戶將輸入數據/文本到UITextField – John 2012-03-23 08:41:18
實際上發生的是什麼意思,輸入數據/文本到UITextField/UITextView我滾動uitableview。那時某些細胞會消失,如果再次向相反的方向滾動細胞將會出現,那麼我以前輸入的那個,就是輸了。數據持久性是主要問題。 – John 2012-03-23 08:50:17
我應該寫這個UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath]; – John 2012-03-23 08:54:25