iOS - 如何在第一次加载表格时在 UITableView 中设置复选标记

发布于 2024-12-14 05:13:54 字数 1069 浏览 0 评论 0原文

我是一名 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];
}

I am an iOS newbie. I am using a checkmark to in a UITableView, and storing the checked value in a local database. When I load the app for the first time, I want to set the value of the checkmark depending on which value exists in the db. How would I do that? Presently this is what I do -

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

and then in didSelectRowAtIndexPath -

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2024-12-21 05:13:55

您是简单地询问如何以及何时设置复选标记,还是询问如何从数据库(例如核心数据)填充表?

从您的代码中,您表示的唯一数据位于 [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;

Are you asking simply how and when to set the checkmark or are you asking how to populate a table from a database (eg Core Data)?

From your code, the only data you represent is in [self countryNames] and it's not clear under what circumstances you'd want the cell to display a checkmark. Whatever that is, just check the condition and set the checkmark when you are configuring your cell for data (after your "Set up the cell..." comment).

Example if you stored the users country and checked that cell:

// 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;
请帮我爱他 2024-12-21 05:13:55

如果您有静态表数据,则只需存储所选表视图单元格的部分和行即可。如果您有动态数据,则需要在数据库中存储该选定单元格的唯一数据,并将其与加载时单元格的内容进行比较。当您在 cellForRowAtIndexPath: 中加载单元格时,只需将该单元格的附件设置为 UITableViewCellAccessoryCheckmark 并设置 self.lastIndexPath = indexPath 以便稍后进行比较。

另外,我通常使用 [indexPath isEqual:self.lastIndexPath] 而不是 compare:。两种方式都没有真正的区别,只是为了可读性。

If you have static table data, then you simply need to store the section and row of the selected table view cell. If you have dynamic data, then you will need to store the unique data for that selected cell in the database and compare that with the cell's content on load. When you are loading your cells in cellForRowAtIndexPath:, simply set that cell's accessory to UITableViewCellAccessoryCheckmark as well as set self.lastIndexPath = indexPath for comparison later.

Also, I typically use [indexPath isEqual:self.lastIndexPath] instead of compare:. Doesn't really make a difference either way, just for readability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文