如何将UITableView
设置为默认为无选择? indexPath.row = -1
或类似的东西?UITableView将其设置为无选择
这里是我创建的表码:
UITableView* tblThisTable = [[UITableView alloc] initWithFrame:CGRectMake(elementX, elementY, elementW, elementH)];
tblThisTable.rowHeight = 30.0;
tblThisTable.layer.borderColor = [colorManager setColor:51.0 :51.0 :51.0].CGColor;
tblThisTable.layer.borderWidth = 1.0;
tblThisTable.delegate = self;
tblThisTable.dataSource = self;
,我的实的委托协议:
#pragma mark - Table View Management
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return arrStates.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [arrStates objectAtIndex:indexPath.row];
cell.textLabel.textColor = [colorManager setColor:66.0 :66.0 :66.0];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:20.0];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
strSelectedState = [arrStates objectAtIndex:indexPath.row];
}
我有一个数据管理器类,我检查用户输入的值之前,将他们保存到plist。下面是相关表的代码(我只有国表):
...
} else if ([[_arrAllElements objectAtIndex:i] isMemberOfClass:[UITableView class]]) {
UITableView* tblThisTable = (UITableView*)[_arrAllElements objectAtIndex:i];
strElementKey = @"State";
int selectedRow = tblThisTable.indexPathForSelectedRow.row;
if(selectedRow > -1) {
strElementValue = [arrStates objectAtIndex:selectedRow];
} else {
strElementValue = @"No Entry";
}
...
所以,如果用户点击保存按钮,而没有选择在表中的任何东西,我仍然得到了selectedRow的0值,但没有显示为选中。我正在考虑UISegementedControls,你可以将它们设置为-1以便没有选择。不应该有同样的表吗?
如何'strSelectedState'定义?如果用户没有选择一行,它将保持默认状态。 – jszumski
strSelectedState被定义为零,但我没有检查。我有一个数据管理器类,在用户保存时传递我需要的所有对象,请参阅上面的其他编辑以查看我如何检查表值 – PruitIgoe
您正在检查'didSelectRowAtIndexPath:',但是您还提到了在发生错误时在用户进行任何行选择之前保存(“在我的测试中,我不碰表,但结果总是indexPath.row = 0”)。什么代码收到'indexPath.row = 0'? – jszumski