我有一个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;
}
此外,tableview是动态的,当然是因为tableviewcells正在由消费者在后端设置的任何东西填充。 – TheChef 2015-01-26 20:53:45
非常混乱。如果你有单元并想知道索引路径,可以使用'indexPathForCell'。但是这听起来有一个数组索引,并希望将其与索引路径相关联 - 这是数据源对象的责任。 – 2015-01-26 21:01:35
是的,理解...我试图使用这样的东西 NSIndexPath * newIndex = [NSIndexPath indexPathForRow:0 inSection:0]; DBSectionDetailsTableViewCell * cell2 =(DBSectionDetailsTableViewCell *)[listTable cellForRowAtIndexPath:newIndex]; 在我的“保存”功能,但我仍然返回“零/无”的一切。我认为去按钮标记的路线可能会更好,我只是想看看我是否可以使用NSIndex来实现我想要的,而不用做所有的标记和附加的函数调用等。 – TheChef 2015-01-26 21:19:02