iOS - 如何在第一次加载表格时在 UITableView 中设置复选标记
我是一名 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是简单地询问如何以及何时设置复选标记,还是询问如何从数据库(例如核心数据)填充表?
从您的代码中,您表示的唯一数据位于
[self countryNames]
中,并且不清楚在什么情况下您希望单元格显示复选标记。无论是什么,只需在配置数据单元格时检查条件并设置复选标记(在“设置单元格...”注释之后)。例如,如果您存储了用户所在国家/地区并检查了该单元格:
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:
如果您有静态表数据,则只需存储所选表视图单元格的部分和行即可。如果您有动态数据,则需要在数据库中存储该选定单元格的唯一数据,并将其与加载时单元格的内容进行比较。当您在
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 toUITableViewCellAccessoryCheckmark
as well as setself.lastIndexPath = indexPath
for comparison later.Also, I typically use
[indexPath isEqual:self.lastIndexPath]
instead ofcompare:
. Doesn't really make a difference either way, just for readability.