如何将 aUITableView默认设置为无选择?  indexPath.row = -1或类似的东西?
这是我创建表的代码:
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 以不进行选择。桌子不应该做同样的事情吗?