4

UITableViewデフォルトを選択なし に設定するにはどうすればよいですか? 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 に設定して選択できないようにすることを考えていました。テーブルも同様にすべきではありませんか?

4

5 に答える 5

1

行をどのように保存していますか?初期化されていないNSInteger/intを保存している場合、デフォルトで0.


テーブルに を要求するときは、プロパティを取得する前にindexPathForSelectedRowそれがあるかどうかを確認する必要があります (パスの は常に になります) 。この更新されたコードは、必要なことを行う必要があります。nilrowrownil0

NSIndexPath *selectedPath = tblThisTable.indexPathForSelectedRow;
if (selectedPath != nil) { 
    strElementValue = [arrStates objectAtIndex:selectedPath.row];
} else {
    strElementValue = @"No Entry";
}
于 2013-04-16T13:24:51.100 に答える
0

このコードを .m ファイルに入れると、選択された行の選択が解除されます。

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

}
于 2013-04-16T13:00:17.297 に答える
0

すべての UITableView をデフォルトで選択なしに設定する場合 (スクロールもタッチ不可)

次に、テーブルビューのプロパティを使用します-

tableView.userInteractionEnabled=NO;

そして、セルの選択を止めたい場合は、

cell.selectionStyle=UITableViewCellSelectionStyleNone;
于 2013-04-18T13:06:53.323 に答える