2013-04-16 27 views
4

如何将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以便没有选择。不应该有同样的表吗?

+0

如何'strSelectedState'定义?如果用户没有选择一行,它将保持默认状态。 – jszumski

+0

strSelectedState被定义为零,但我没有检查。我有一个数据管理器类,在用户保存时传递我需要的所有对象,请参阅上面的其他编辑以查看我如何检查表值 – PruitIgoe

+0

您正在检查'didSelectRowAtIndexPath:',但是您还提到了在发生错误时在用户进行任何行选择之前保存(“在我的测试中,我不碰表,但结果总是indexPath.row = 0”)。什么代码收到'indexPath.row = 0'? – jszumski

回答

1

如何保存行?如果您保存未初始化的NSInteger/int,这可能解释为什么它的默认值为0


当你问表indexPathForSelectedRow,你需要检查它是否nil抓住row属性之前(一nil路径row永远是0)。这个更新的代码应该做你需要的:

NSIndexPath *selectedPath = tblThisTable.indexPathForSelectedRow; 
if (selectedPath != nil) { 
    strElementValue = [arrStates objectAtIndex:selectedPath.row]; 
} else { 
    strElementValue = @"No Entry"; 
} 
+0

我现在没有保存任何东西 - 视图没有加载我正在建立表格,点击时我有一个保存按钮,我收集所有相关数据 - 在我的测试中,我不碰表格,但结果总是indexPath .row = 0。表格不显示行选择(它以蓝色突出显示)。查看我对OP的编辑。 – PruitIgoe

+0

你可以发布你收集这些结果的代码吗?我不知道你是否正在更新'tableView:didSelectRowAtIndexPath:'中的ivar,或者如果你要求tableview为'indexPathForSelectedRow'。如果是后者,那么可能会返回一个'nil'路径,并且'indexPath.row'将等于'0'。 – jszumski

+0

编辑OP - 我正在使用didSelectRowAtIndexPath – PruitIgoe

0

将这段代码放到.m文件中,它将取消选中所选行。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
+0

我应该更清楚一点 - 我能在桌子上做这个吗?我有一张美国各州的表格,当我去保存用户条目时,我没有选择任何东西时,它默认为第一行(阿拉巴马州)。他们不必选择一个状态,但它也不能默认为一个状态。 – PruitIgoe

0

下面的解决方案将工作。编辑您的代码创建的细胞:

// 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.selectionStyle=UITableViewCellSelectionStyleNone; 
    } 

    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; 
} 

我添加了一行:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

更多信息可以在这里找到: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html

+0

我不认为他意味着细胞选择状态,他的意思是选择在“用户的选择”中。 – jszumski

0

如果你想设置的所有的UITableView为默认到没有选择(滚动也不可触摸)

然后使用表视图属性 -

tableView.userInteractionEnabled=NO; 

如果你想阻止细胞再选择,

cell.selectionStyle=UITableViewCellSelectionStyleNone; 
+0

不想做任何事情,我只是希望它默认在加载时没有选择。 – PruitIgoe

0

在斯威夫特

// IF tableview selected row 
let selectedIndexPath = tableView_Main.indexPathForSelectedRow 

if selectedIndexPath != nil { 

    tableView_Main.deselectRowAtIndexPath(selectedIndexPath!, animated: true) 

} 
相关问题