使用静态单元格创建设置场景 - 附上屏幕截图和简单测试用例
Creating settings scene using static cells - screenshots and simple test case attached
我准备了一个非常简单的测试用例项目MyStaticCells at GitHub来演示我的问题。
我正在尝试在 iOS 应用程序中创建设置屏幕 - 通过使用静态单元格(此处 fullscreen picture):
您可以在下面更仔细地看到设置视图控制器(此处 fullscreen picture)-
设置场景应该为用户提供 3 个操作:
- 声音 on/off
- 发送电子邮件给支持
- 恢复购买
因为我不知道如何在使用静态单元格时在 table 视图控制器的顶部制作一些 space - 我使用了 4 个静态单元格并放置了齿轮图标,顶部静态单元格中的标题 设置 和 关闭 按钮:
我的2个问题:
- 虽然顶部静态单元格中的 Close 按钮有效 - 它看起来不太好 - 因为 "header" 也会滚动,当用户尝试滚动屏幕:
- 当用户点击一个单元格时——例如切换声音——这有效。但它只有在用户点击另一个单元格后才有效:
这是我的源代码 -
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#define THEME_COLOR_GRAY_TEXT [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0]
#define THEME_COLOR_BLUE [UIColor colorWithRed:19.0/255 green:175.0/255 blue:207.0/255 alpha:1.0]
@interface SettingsViewController : UITableViewController <MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, weak) IBOutlet UILabel *labelTitle;
@property (nonatomic, weak) IBOutlet UILabel *labelButtonSounds;
@property (nonatomic, weak) IBOutlet UILabel *labelButtonEmail;
@property (nonatomic, weak) IBOutlet UILabel *labelButtonRestore;
@property (nonatomic, weak) IBOutlet UILabel *labelButtonSoundsOnOff;
@property (weak, nonatomic) IBOutlet UITableViewCell *cellSounds;
@property (weak, nonatomic) IBOutlet UITableViewCell *cellSupport;
@property (weak, nonatomic) IBOutlet UITableViewCell *cellRestore;
- (IBAction)clickedClose:(id)sender;
@end
#import "SettingsViewController.h"
@interface SettingsViewController ()
@property (nonatomic, assign) BOOL soundOn;
@end
@implementation SettingsViewController
......
- (void)refreshView
{
if (self.soundOn)
{
self.labelButtonSoundsOnOff.text = @"On";
self.labelButtonSoundsOnOff.textColor = THEME_COLOR_BLUE;
}
else
{
self.labelButtonSoundsOnOff.text = @"Off";
self.labelButtonSoundsOnOff.textColor = THEME_COLOR_GRAY_TEXT;
}
NSLog(@"refreshView: %d", self.soundOn);
}
- (void)clickedSound
{
NSLog(@"clickedSound");
self.soundOn = !self.soundOn;
[self refreshView];
}
- (IBAction)clickedClose:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Table View delegate
- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *clickedCell =
[self.tableView cellForRowAtIndexPath:indexPath];
if (clickedCell == self.cellSounds)
[self clickedSound];
else if (clickedCell == self.cellSupport)
[self clickedSupport];
else if (clickedCell == self.cellRestore)
[self clickedRestore];
}
@end
你看,我在这个问题上下了很大的功夫,请大家帮忙
在我的真实项目中,我有许多场景的自定义按钮被文本标签重叠 - 我想用静态单元格替换它们。
更新:
经过 rdelmar 和 Viral Savaj 的精彩解答(谢谢),我已将该方法重命名为 tableView:didSelectRowAtIndexPath:
,禁用弹跳和禁用顶行选择(以便单击时不会改变颜色):
我还给顶行设置了较小的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([indexPath row] == 0 ? 70 : 120);
}
并清除选择:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *clickedCell = [self.tableView cellForRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (clickedCell == self.cellSounds) {
[self clickedSound];
} else if (clickedCell == self.cellSupport) {
[self clickedSupport];
} else if (clickedCell == self.cellRestore) {
[self clickedRestore];
}
}
我还有2个小问题:
- 如何去除单元格之间的灰色线条?
- 如果我想让标题和 关闭 按钮不在顶行 - 这样它们就不会被滚动并始终可见 - 有吗一种在 Interface Builder 中实现的方法 "visually"?
对于问题 1,您可以将 标题设置 和关闭按钮放在 HeaderView
中
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
问题 2.1 的解决方案
只需从 Storyboard 图片右侧的 属性 检查器中删除所有类型的 bounce,如反弹、反弹缩放、垂直反弹。
问题 2.2 的解决方案
改变声音后可以立即重新加载UITableView(在改变声音的函数中)。
HTH,享受编码吧!!
在 table 视图顶部制作 space 的方法是使用 contentInset 属性,
self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
您的第二个问题是因为您错误地实施了 tableView:didDeselectRowAtIndexPath:
而不是 tableView:didSelectRowAtIndexPath:
。
我准备了一个非常简单的测试用例项目MyStaticCells at GitHub来演示我的问题。
我正在尝试在 iOS 应用程序中创建设置屏幕 - 通过使用静态单元格(此处 fullscreen picture):
您可以在下面更仔细地看到设置视图控制器(此处 fullscreen picture)-
设置场景应该为用户提供 3 个操作:
- 声音 on/off
- 发送电子邮件给支持
- 恢复购买
因为我不知道如何在使用静态单元格时在 table 视图控制器的顶部制作一些 space - 我使用了 4 个静态单元格并放置了齿轮图标,顶部静态单元格中的标题 设置 和 关闭 按钮:
我的2个问题:
- 虽然顶部静态单元格中的 Close 按钮有效 - 它看起来不太好 - 因为 "header" 也会滚动,当用户尝试滚动屏幕:
- 当用户点击一个单元格时——例如切换声音——这有效。但它只有在用户点击另一个单元格后才有效:
这是我的源代码 -
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#define THEME_COLOR_GRAY_TEXT [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0]
#define THEME_COLOR_BLUE [UIColor colorWithRed:19.0/255 green:175.0/255 blue:207.0/255 alpha:1.0]
@interface SettingsViewController : UITableViewController <MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, weak) IBOutlet UILabel *labelTitle;
@property (nonatomic, weak) IBOutlet UILabel *labelButtonSounds;
@property (nonatomic, weak) IBOutlet UILabel *labelButtonEmail;
@property (nonatomic, weak) IBOutlet UILabel *labelButtonRestore;
@property (nonatomic, weak) IBOutlet UILabel *labelButtonSoundsOnOff;
@property (weak, nonatomic) IBOutlet UITableViewCell *cellSounds;
@property (weak, nonatomic) IBOutlet UITableViewCell *cellSupport;
@property (weak, nonatomic) IBOutlet UITableViewCell *cellRestore;
- (IBAction)clickedClose:(id)sender;
@end
#import "SettingsViewController.h"
@interface SettingsViewController ()
@property (nonatomic, assign) BOOL soundOn;
@end
@implementation SettingsViewController
......
- (void)refreshView
{
if (self.soundOn)
{
self.labelButtonSoundsOnOff.text = @"On";
self.labelButtonSoundsOnOff.textColor = THEME_COLOR_BLUE;
}
else
{
self.labelButtonSoundsOnOff.text = @"Off";
self.labelButtonSoundsOnOff.textColor = THEME_COLOR_GRAY_TEXT;
}
NSLog(@"refreshView: %d", self.soundOn);
}
- (void)clickedSound
{
NSLog(@"clickedSound");
self.soundOn = !self.soundOn;
[self refreshView];
}
- (IBAction)clickedClose:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Table View delegate
- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *clickedCell =
[self.tableView cellForRowAtIndexPath:indexPath];
if (clickedCell == self.cellSounds)
[self clickedSound];
else if (clickedCell == self.cellSupport)
[self clickedSupport];
else if (clickedCell == self.cellRestore)
[self clickedRestore];
}
@end
你看,我在这个问题上下了很大的功夫,请大家帮忙
在我的真实项目中,我有许多场景的自定义按钮被文本标签重叠 - 我想用静态单元格替换它们。
更新:
经过 rdelmar 和 Viral Savaj 的精彩解答(谢谢),我已将该方法重命名为 tableView:didSelectRowAtIndexPath:
,禁用弹跳和禁用顶行选择(以便单击时不会改变颜色):
我还给顶行设置了较小的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([indexPath row] == 0 ? 70 : 120);
}
并清除选择:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *clickedCell = [self.tableView cellForRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (clickedCell == self.cellSounds) {
[self clickedSound];
} else if (clickedCell == self.cellSupport) {
[self clickedSupport];
} else if (clickedCell == self.cellRestore) {
[self clickedRestore];
}
}
我还有2个小问题:
- 如何去除单元格之间的灰色线条?
- 如果我想让标题和 关闭 按钮不在顶行 - 这样它们就不会被滚动并始终可见 - 有吗一种在 Interface Builder 中实现的方法 "visually"?
对于问题 1,您可以将 标题设置 和关闭按钮放在 HeaderView
中-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
问题 2.1 的解决方案
只需从 Storyboard 图片右侧的 属性 检查器中删除所有类型的 bounce,如反弹、反弹缩放、垂直反弹。
问题 2.2 的解决方案
改变声音后可以立即重新加载UITableView(在改变声音的函数中)。
HTH,享受编码吧!!
在 table 视图顶部制作 space 的方法是使用 contentInset 属性,
self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
您的第二个问题是因为您错误地实施了 tableView:didDeselectRowAtIndexPath:
而不是 tableView:didSelectRowAtIndexPath:
。