Joh*_*ohn 2 iphone xcode objective-c uitableview
我正在尝试实现UITableview基于应用程序.在我的tableView他们是10部分,每个部分有一行.我想实现每个部分都有不同的类型ContentView(1-8相同的ContentView第9节不同ContentView).我做了这个代码.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:1.在UITextField/ UITextView 我滚动后键入一些东西UITableView.那个时候UITableViewCell(UITextField/ UITextView)中的所有数据都丢失了,除了最后一个单元格数据.2.如果我创建单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)
代替
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
数据会重复.我怎么能过来这个问题呢?
这一行:
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
永远不应出现在您的数据源cellForRowAtIndexPath方法中.
除此之外,您的代码没问题,除了您没有在任何地方设置文本字段值.您需要一个模型(例如10个文本字段值的字符串数组).编辑文本字段时应更新此模型,并在上面的方法中将值从模型中复制回文本字段的text属性:
textfield.text = [self.modelArray objectAtIndex:indexPath.section];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8497 次 |
| 最近记录: |