1

私はiOSの初心者です。UITableViewでチェックマークを使用して、チェックした値をローカルデータベースに保存しています。初めてアプリをロードするときに、データベースに存在する値に応じてチェックマークの値を設定したいと思います。どうすればいいですか?現在、これは私がしていることです-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath compare:self.lastIndexPath] == NSOrderedSame) 
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} 
else 
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
// Set up the cell...
NSString *cellValue = [[self countryNames] objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
}

そしてdidSelectRowAtIndexPathで-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// some stuff
self.lastIndexPath = indexPath;
[tableView reloadData];
}
4

2 に答える 2

3

チェックマークをいつどのように設定するか、またはデータベース (コア データなど) からテーブルにデータを入力する方法を尋ねているだけですか?

あなたのコードから、あなたが表す唯一のデータがあり[self countryNames]、セルにチェックマークを表示する状況が明確ではありません。それが何であれ、セルをデータ用に構成するときに、条件を確認してチェックマークを設定するだけです(「セルの設定...」コメントの後)。

ユーザーの国を保存し、そのセルをチェックした場合の例:

// get the current country name
NSString *cellValue = [[self countryNames] objectAtIndex:indexPath.row];

// configure the cell
cell.textLLabel.text = cellValue;
UITableViewCellAccessoryType accessory = UITableViewCellAccessoryNone;
if ([cellValue isEqualToString:self.usersCountry]) {
    accessory = UITableViewCellAccessoryCheckmark;
}
cell.accessoryType = accessory;
于 2011-11-07T23:25:04.613 に答える
0

静的なテーブル データがある場合は、選択したテーブル ビュー セルのセクションと行を保存するだけです。動的データがある場合は、選択したセルの一意のデータをデータベースに保存し、それをロード時にセルのコンテンツと比較する必要があります。セルを にロードするときはcellForRowAtIndexPath:、そのセルのアクセサリを に設定し、後で比較UITableViewCellAccessoryCheckmarkできるように設定するだけです。self.lastIndexPath = indexPath

また、私は通常、[indexPath isEqual:self.lastIndexPath]代わりにcompare:. 読みやすさのためだけに、どちらの方法でも実際には違いはありません。

于 2011-11-07T23:23:31.467 に答える