我准备了一个非常简单的测试用例项目MyStaticCells at GitHub来证明我的问题。
我正在尝试在 iOS 应用程序中创建一个设置屏幕 - 通过使用静态单元格(此处为 fullscreen picture):
在下方您可以更近距离地看到设置 View Controller (此处为 fullscreen picture)-
设置场景应该为用户提供 3 个操作:
- 声音开/关
- 发送电子邮件给支持人员
- 恢复购买
因为我不知道如何在使用静态单元格时在 TableView Controller 顶部留出一些空间 - 我使用了 4 个静态单元格并放置了齿轮图标,标题设置以及顶部静态单元格中的关闭按钮:
我的 2 个问题:
- 虽然顶部静态单元格中的关闭按钮有效 - 它看起来不太好 - 因为“标题”也会滚动,当用户尝试滚动屏幕时:
- 当用户点击一个单元格时 - 例如切换声音 - 这会起作用。但它只有在用户点击另一个单元格后才有效:
这是我的源代码-
#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];
}
}
我还有两个小问题:
- 如何去掉单元格之间的灰线?
- 如果我想让标题和关闭按钮不在顶行 - 这样它们就不会被滚动并始终可见 - 有没有办法做到这一点在 Interface Builder 中“视觉上”?
最佳答案
在表格 View 顶部留出空间的方法是使用 contentInset 属性,
self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
您的第二个问题是因为您错误地实现了 tableView:didDeselectRowAtIndexPath:
而不是 tableView:didSelectRowAtIndexPath:
。
关于ios - 使用静态单元格创 build 置场景 - 附上屏幕截图和简单测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29678978/