2013-04-16 35 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) 

} 
相關問題