我有一个6行的自定义表视图。5行中有UITextFields,1行中有UISwitch。
我添加如下文本框:
switch (indexPath.row) {
case 0:
txtLabel.text = NSLocalizedString(@"Name:", @"");
txtField.tag=1;
[txtField becomeFirstResponder];
[cell.contentView addSubview:txtField];
[txtField release];
break;在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {该开关添加如下:
case 3:
txtLabel.text = NSLocalizedString(@"Male:", @"");
UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;
[mySwitch setTag:6];
break;我用另一种方法读取值,如下所示:
UITextField *daName = (UITextField *)[self.view viewWithTag:1];但是由于某种原因,一旦我添加了UISwitch,daName值就会变为null。其他值读得很好(所以标签2是ok的,标签3是ok的等等)。
我在这里错过了什么?因为当我移除UISwitch时,我从标记1中得到一个值.
提前感谢!
编辑
我只看到,当我在我的表视图中有6行时,带有标记id 1的元素就会获得null.只要我有5行,我就能得到所有的值.
发布于 2011-03-28 09:14:04
好的,我已经添加了一个mutablearray,并将文本字段添加到数组中。这样我就不用再用这个标签了.
发布于 2011-03-11 14:15:26
我确实在堆栈溢出的某个地方读到,当您设置标记时,不应该使用0或1,我不记得在哪里,但是我认为它提到了一些关于默认的东西?也许值得一查..。
发布于 2011-12-23 06:18:14
UISwitch *daName = (UISwitch *)[self.view viewWithTag:tag];
if (daName.on){
// handle the switch of particular of tag
}https://stackoverflow.com/questions/5266292
复制相似问题