为什么在UITableView';s单元格仅适用于第一行?
我正在遍历我桌子上的所有单元格。每个单元格都包含一个UITextField。我想验证文本字段中的数据,但我只从表的第一行获取有效字符串。所有其他后续行的文本返回为什么在UITableView';s单元格仅适用于第一行?,uitableview,for-loop,cell,Uitableview,For Loop,Cell,我正在遍历我桌子上的所有单元格。每个单元格都包含一个UITextField。我想验证文本字段中的数据,但我只从表的第一行获取有效字符串。所有其他后续行的文本返回null。有什么想法吗 -(IBAction)saveContactToDB:(UIBarButtonItem *)sender { //TODO Loop through each row and test the data. // At least one name has to b
null
。有什么想法吗
-(IBAction)saveContactToDB:(UIBarButtonItem *)sender
{
//TODO Loop through each row and test the data.
// At least one name has to be entered. If this fails a UIAlert is prompted; otherwise, write to DB
NSLog(@"Attempting to save contact");
NSInteger contactID;
for (NSInteger section = 0; section < [_contactInfoTable numberOfSections]; ++section)
{
for (NSInteger row = 0; row < [_contactInfoTable numberOfRowsInSection:section]; ++row)
{
ContactFieldCell *cell = (ContactFieldCell *)[_contactInfoTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:section
inSection:row]];
NSLog(@"%@", cell.cellTextField.text);
NSLog(@"%@", cell.cellTextField.placeholder);
//Make sure at least one name is entered
//When i = 0 && j = 0 it means we are on the first row of the name section
if (section == 0 && row ==0 && cell.cellTextField.text == @"")
{
NSLog(@"Name is required");
//Present alert
goto ALERTSHOWN;
}
else
{
//Write the data to the DB in its respective place
if (section == 0)
{
//Names
contactID = [APP.localDatabase saveContactName:cell.cellTextField.text];
}
else if (section == 1)
{
//Position
[APP.localDatabase saveContactInfoValue:cell.cellTextField.text
WithType:POSITION
ForContact:contactID
AtOrder:row];
}
else if (section == 2)
{
//Schedule
[APP.localDatabase saveContactInfoValue:cell.cellTextField.text
WithType:SCHEDULE
ForContact:contactID
AtOrder:row];
}
else if (section == 3)
{
//Phone number
[APP.localDatabase saveContactInfoValue:cell.cellTextField.text
WithType:PHONE
ForContact:contactID
AtOrder:row];
}
else if (section == 4)
{
//Miscellaneous
[APP.localDatabase saveContactInfoValue:cell.cellTextField.text
WithType:MISCELLANEOUS
ForContact:contactID
AtOrder:row];
}
}
}
}
[self.view removeFromSuperview];
ALERTSHOWN:;
}
-(iAction)saveContactToDB:(UIBarButtonItem*)发送方
{
//TODO循环遍历每行并测试数据。
//必须至少输入一个名称。如果输入失败,将提示UIAlert;否则,写入DB
NSLog(@“正在尝试保存联系人”);
NSInteger-contactID;
对于(NSInteger section=0;section<[\u contactInfoTable numberOfSections];++section)
{
对于(NSInteger row=0;row<[\u contactInfoTable numberOfRowsInSection:section];++行)
{
ContactFieldCell*cell=(ContactFieldCell*)[\u contactInfoTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:节
分类:行]];
NSLog(@“%@”,cell.cellTextField.text);
NSLog(@“%@”,cell.cellTextField.placeholder);
//确保至少输入了一个名称
//当i=0&&j=0时,表示我们位于名称部分的第一行
if(section==0&&row==0&&cell.cellTextField.text==@“”)
{
NSLog(@“需要名称”);
//当前警报
后藤;
}
其他的
{
//将数据写入数据库的相应位置
如果(节==0)
{
//名字
contactID=[APP.localDatabase saveContactName:cell.cellTextField.text];
}
else if(节==1)
{
//位置
[APP.localDatabase saveContactInfoValue:cell.cellTextField.text
WithType:位置
ForContact:contactID
原子序:行];
}
否则如果(节==2)
{
//时间表
[APP.localDatabase saveContactInfoValue:cell.cellTextField.text
WithType:时间表
ForContact:contactID
原子序:行];
}
否则如果(节==3)
{
//电话号码
[APP.localDatabase saveContactInfoValue:cell.cellTextField.text
WithType:电话
ForContact:contactID
原子序:行];
}
否则如果(节==4)
{
//杂
[APP.localDatabase saveContactInfoValue:cell.cellTextField.text
WithType:杂项
ForContact:contactID
原子序:行];
}
}
}
}
[self.view removeFromSuperview];
如图所示:;
}
通过方法cellForRowAtIndexPath获取单元格时,变量部分作为行传输,变量部分作为行传输。应该是:
ContactFieldCell *cell = (ContactFieldCell *)[_contactInfoTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
我翻转了ContactFieldCell*cell=(ContactFieldCell*)[\u contactInfoTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:section Instition:row]]中的行和节的值;有趣的错误。。。