【发布时间】:2013-05-07 21:28:22
【问题描述】:
我正在使用加载 UITableView 的菜单样式的幻灯片。 - ECSlidingViewController
我在表格视图设置中有大约 7 个单元格,如下所示:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];
UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1];
[cell.contentView addSubview:topSplitterBar];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:196.0/255.0 green:204.0/255.0 blue:218.0/255.0 alpha:1];
cell.textLabel.font = [UIFont systemFontOfSize:18.0f];
cell.textLabel.shadowColor = [UIColor colorWithRed:27.0/255.0 green:31.0/255.0 blue:41.0/255.0 alpha:1];
cell.textLabel.shadowOffset = CGSizeMake(0, 1);
UIView *selectedBg = [[UIView alloc] initWithFrame:cell.frame];
selectedBg.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
cell.selectedBackgroundView = selectedBg;
如果这是当前显示的控制器,那么将单元格显示为 selectedBg 的最佳方式是什么?
例如,我可以访问以下内容:
if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
但是,我不确定在哪里进行设置的最佳做法是?我可以在开关盒中进行单元标签设置...例如:
switch ( indexPath.row ) {
case 0: {
if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
}
cell.textLabel.text = NSLocalizedString(@"LMGames", @"Left Menu - Games");
break ;
但是,当从菜单中选择一个新项目时,我每次都需要重新加载表格,这样好吗?每次选择一个单元格时填写一个self.tableView reloadData,还是有更好的方法来解决这个问题?
【问题讨论】:
-
或者也只是在
didSelectRowAtIndexPath方法中设置selectedBg。 -
我试图添加这个' UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1]; ' 在 didSelectRowAtIndexPath 部分,但没有发生变化?
-
@iWasRobbed - 好的,我可以使用 BOOL 但仅使用 self tableView reloadData。如果当前显示单元格(因此),这是我检查单元格创建。问题是,这是实现这一目标的最佳方式吗?如果我更改了 didSelectRowAtIndexPath 中的显示,那么如何确保其他单元格背景正确...而不重新加载 tableView 的数据并在那里检查。
-
只保留对当前选定行和先前选定行的整数引用,然后只刷新这些行stackoverflow.com/a/5418726/308315
-
@iWasRobbed - 谢谢,请提交作为不接受的答案。再次感谢
标签: ios ios5 uitableview