杀掉每一个人
我已经创建了一个类似这样的数据输入屏幕,其中每一行都包含一个文本字段
这是我为每个单元格编写的代码,其中CustomCellStudentData
是我为标签创建框架的单元格类
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = @"Cell";
CustomCellStudentData *cell = (CustomCellStudentData*)[tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
switch(indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
cell.accessoryView = tfText[0];
tfText[0].delegate=self;
tfText[0].font = [UIFont systemFontOfSize:14];
tfText[0].placeholder =@"<Student Name>";
cell.primaryLabel.text =@"Student Name: ";
}
break;
case 1:
{
tfText[1] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
cell.accessoryView = tfText[1];
tfText[1].delegate=self;
tfText[1].font = [UIFont systemFontOfSize:14];
tfText[1].placeholder =@"<Student Age>";
tfText[1].keyboardType = UIKeyboardTypeNumbersAndPunctuation;
cell.primaryLabel.text =@"Student Age: ";
}
break;
case 2:
{
cell.primaryLabel.text =@"Gender: ";
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(125,22, 170, 25);
[segmentedControl insertSegmentWithTitle:@"Male" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:@"Female" atIndex:1 animated:YES];
segmentedControl.selectedSegmentIndex = 1;
[segmentedControl setMomentary:NO];
[segmentedControl addTarget:self action:@selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView =segmentedControl;
}
break;
case 3:
{
tfText[3] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
cell.accessoryView = tfText[3];
tfText[3].delegate=self;
tfText[3].font = [UIFont systemFontOfSize:14];
tfText[3].placeholder =@"<dd/mm/yyyy>";
tfText[3].keyboardType = UIKeyboardTypeNumbersAndPunctuation;
cell.primaryLabel.text =@"Date Of Birth: ";
}
}
}
当数据变为空时,我该如何解决这个问题,谁能帮我,谢谢
发布于 2011-06-03 20:53:46
每次调用cellForRowAtIndexPath:
时都会创建文本字段。我认为您可以通过在其他地方创建文本字段来解决这个问题,可能是在viewDidLoad
或类似的方法中,并在调用cellForRowAtIndexPath:
时设置附件视图。例如,
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
tfText[0].delegate=self;
tfText[0].font = [UIFont systemFontOfSize:14];
tfText[0].placeholder =@"<Student Name>";
将顶部的四行移动到viewDidLoad
,并且只创建一次它们。让下面这行保持原样。
cell.accessoryView = tfText[0];
发布于 2011-06-03 20:53:53
因为每次创建单元格时都会重新创建文本字段。
取第一个单元格(在索引0处)
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
cell.accessoryView = tfText[0];
tfText[0].delegate=self;
tfText[0].font = [UIFont systemFontOfSize:14];
tfText[0].placeholder =@"<Student Name>";
cell.primaryLabel.text =@"Student Name: ";
第一次创建表格时,您将创建一个文本视图并将其添加到单元格中。然后,用户将在单元格中输入他们的数据(在本例中是他们的名字)。
然后,他们将表格向下滚动,然后向上滚动。
表视图将再次调用tableView:cellForRowAtIndexPath:。您的代码将索引0处的文本字段替换为一个新的空字段。
在您的switch语句中尝试类似以下内容:
if (NULL == tfText[0]) {
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
tfText[0].delegate=self;
tfText[0].font = [UIFont systemFontOfSize:14];
tfText[0].placeholder =@"<Student Name>";
}
cell.accessoryView = tfText[0];
cell.primaryLabel.text =@"Student Name: ";
发布于 2011-06-03 20:29:17
dequeueReusableCellWithIdentifier:nil
我相信这就是问题所在,你可能会得到其他类型的细胞。您应该为在该表中使用的每种单元格设置一个单元格标识符。
https://stackoverflow.com/questions/6226942
复制相似问题