2015-01-26 92 views
-1

我有一个tableviewcell与5个不同的单元格。每个视图都有一个标签,其中包含以下内容之一。 1个文本框,1个文本,视图1,开关或1个滑块。我正在尝试在索引路径中查找行的tableviewcell,以便我可以检索用户输入的信息。以下是我对单元和不同开关的代码。如果可能,我只需要知道如何实现这一点。谢谢(页面)我在考虑只是标记元素,而不是为行寻找单元格,这就是为什么您会看到标记代码。如何获得从数组填充的tableviewcell的索引路径?

-(UITableViewCell *)listCell:(UITableView *)tableView IndexPath:(NSIndexPath*)indexPath 
{ 
static NSString *textFieldIdentifier = @"textFieldCell"; 
static NSString *textViewIdentifier = @"textViewCell"; 
static NSString *switchIdentifier = @"switchCell"; 
static NSString *seekBarIdentifier = @"seekBarCell"; 
static NSString *datePickerIdentifier = @"datePickerCell"; 

//DBSectionDetailsTableViewCell *cell = (DBSectionDetailsTableViewCell *) [tableView dequeueReusableCellWithIdentifier:textViewIdentifier]; 
UITableViewCell *cell6 = [tableView dequeueReusableCellWithIdentifier:datePickerIdentifier]; 


results = [listArray objectAtIndex:[indexPath row]]; 


if([indexPath row] < listArray.count) 
{ 
    if (([[results objectAtIndex:2]isEqualToString:@"EditText"])) 
    { 

     DBSectionDetailsTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:textFieldIdentifier forIndexPath:indexPath]; 
     cell2.titleLabel.text = [results objectAtIndex:1]; 
     //title button 
     cell2.textField.tag = [indexPath row]; 
     cell2.textField.delegate = self; 

     [cell2.textField addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventTouchUpInside]; 


    return cell2; 
    } 

    else if (([[results objectAtIndex:2]isEqualToString:@"EditTextLarge"])) 
    { 
     TextViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:textViewIdentifier forIndexPath:indexPath]; 

     // cell = textFieldCell; 
     cell.titleLabel.text = [results objectAtIndex:1]; 
     cell.textView.tag = [indexPath row]; 
     cell.textView.delegate = self; 
     // [cell.textView addTarget:self action:@selector(textViewDidEndEditing:) forControlEvents:UIControlEventTouchUpInside]; 


     return cell; 
    } 

    else if (([[results objectAtIndex:2]isEqualToString:@"Switch"])) 
    { 

     SwitchTableViewCell *cell3 = [tableView dequeueReusableCellWithIdentifier:switchIdentifier forIndexPath:indexPath]; 


     // cell3 = switchCell; 
     cell3.titleLabel.text = [results objectAtIndex:1]; 
     cell3.switchOutlet.tag = [indexPath row]; 


     return cell3; 
    } 

    else if (([[results objectAtIndex:2]isEqualToString:@"SeekBar"])) 
    { 

     SeekBarTableViewCell *cell4 = [tableView dequeueReusableCellWithIdentifier:seekBarIdentifier forIndexPath:indexPath]; 


     //cell4 = seekBarCell; 
     cell4.titleLabel.text = [results objectAtIndex:1]; 
     cell4.slider.tag = [indexPath row]; 

     return cell4; 
    } 

    else if (([[results objectAtIndex:2]isEqualToString:@"DatePicker"])) 
    { 

     DatePickerTableViewCell *cell5 = [tableView dequeueReusableCellWithIdentifier:datePickerIdentifier forIndexPath:indexPath]; 

     //cell5 = datePickerCell; 
     cell5.titleLabel.text = [results objectAtIndex:1]; 
     cell5.datePicker.tag = [indexPath row]; 
     cell5.datePicker.delegate = self; 

     [cell5.datePicker addTarget:self action:@selector(datePickerDidEndEditing:) forControlEvents:UIControlEventTouchUpInside]; 

     return cell5; 
    } 


} 

//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 


return cell6; 

}

+0

此外,tableview是动态的,当然是因为tableviewcells正在由消费者在后端设置的任何东西填充。 – TheChef 2015-01-26 20:53:45

+0

非常混乱。如果你有单元并想知道索引路径,可以使用'indexPathForCell'。但是这听起来有一个数组索引,并希望将其与索引路径相关联 - 这是数据源对象的责任。 – 2015-01-26 21:01:35

+0

是的,理解...我试图使用这样的东西 NSIndexPath * newIndex = [NSIndexPath indexPathForRow:0 inSection:0]; DBSectionDetailsTableViewCell * cell2 =(DBSectionDetailsTableViewCell *)[li​​stTable cellForRowAtIndexPath:newIndex]; 在我的“保存”功能,但我仍然返回“零/无”的一切。我认为去按钮标记的路线可能会更好,我只是想看看我是否可以使用NSIndex来实现我想要的,而不用做所有的标记和附加的函数调用等。 – TheChef 2015-01-26 21:19:02

回答

1

可以使用[tableView cellForRowAtIndexPath:indexPath]获取从表中可见单元格。屏幕外的单元格将从UITableView中删除,您将无法访问它们,因为它们将被放回到可重用单元队列中。


注1:你不应该依靠你UITableView存储当前的数据状态。它抛弃了在屏幕外滚动的单元格,因此如果您不将数据保存在其他地方,它将永远丢失。


注2:此代码是不是特别安全:

results = [listArray objectAtIndex:[indexPath row]]; 
if([indexPath row] < listArray.count) // This is always true. 
             // If it isn't true, the above 
             // objectAtIndex: will crash your app. 
+0

谢谢,它会一直是真的,因为总是会有后端数据...“列表数组”,除非它被黑掉,所有的数据被删除,如果发生那么好......整个应用程序将无法工作因为它基于数据库。 但是,非常感谢 – TheChef 2015-01-26 21:08:13

+0

如果它总是真的,为什么麻烦检查呢? – 2015-01-26 21:15:05

+0

我想我现在可以把它拿出来,但是在开始时,这部分应用程序的后端还没有完成。所以,而不是删除它,我只是把它切换。在完成一个过程结束时,我清理了我的代码,但是这个过程并不完整,所以我仍然有注释行等被去掉。但再次感谢您的额外的眼睛。我想我会标记我试图检索信息的项目。我只是试图避免与之相关的额外功能。 – TheChef 2015-01-26 21:27:27

0

在用户输入其每个单元应该保存的信息,因为你无法到达,一旦细胞离开视图以后再使用。

相关问题