- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的应用程序的最新屏幕截图:
现在我想删除单元格“标题”和两个单元格“(其他项目 1)”和“(其他项目 2)”。我怎样才能正确地做到这一点?我不断收到错误。
这里是MyTableViewController.m(由我编辑并之前从here下载)
#import "MyTableViewController.h"
#define kPickerAnimationDuration 0.40 // duration for the animation to slide the date picker into view
#define kDatePickerTag 99 // view tag identifiying the date picker view
#define kTitleKey @"title" // key for obtaining the data source item's title
#define kDateKey @"date" // key for obtaining the data source item's date value
// keep track of which rows have date cells
#define kDateStartRow 1
#define kDateEndRow 2
static NSString *kDateCellID = @"dateCell"; // the cells with the start or end date
static NSString *kDatePickerID = @"datePicker"; // the cell containing the date picker
static NSString *kOtherCell = @"otherCell"; // the remaining cells at the end
#pragma mark -
@interface MyTableViewController ()
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
// keep track which indexPath points to the cell with UIDatePicker
@property (nonatomic, strong) NSIndexPath *datePickerIndexPath;
@property (assign) NSInteger pickerCellRowHeight;
@property (nonatomic, strong) IBOutlet UIDatePicker *pickerView;
// this button appears only when the date picker is shown (iOS 6.1.x or earlier)
@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton;
@end
#pragma mark -
@implementation MyTableViewController
/*! Primary view has been loaded for this view controller
*/
- (void)viewDidLoad
{
[super viewDidLoad];
// setup our data source
NSMutableDictionary *itemOne = [@{ kTitleKey : @"Title" } mutableCopy];
NSMutableDictionary *itemTwo = [@{ kTitleKey : @"Startdatum",
kDateKey : [NSDate date] } mutableCopy];
NSMutableDictionary *itemThree = [@{ kTitleKey : @"Enddatum",
kDateKey : [NSDate date] } mutableCopy];
NSMutableDictionary *itemFour = [@{ kTitleKey : @"(other item1)" } mutableCopy];
NSMutableDictionary *itemFive = [@{ kTitleKey : @"(other item2)" } mutableCopy];
self.dataArray = @[itemOne, itemTwo, itemThree, itemFour, itemFive];
self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setDateStyle:NSDateFormatterShortStyle]; // show short-style date format
[self.dateFormatter setTimeStyle:NSDateFormatterShortStyle];
// obtain the picker view cell's height, works because the cell was pre-defined in our storyboard
UITableViewCell *pickerViewCellToCheck = [self.tableView dequeueReusableCellWithIdentifier:kDatePickerID];
self.pickerCellRowHeight = pickerViewCellToCheck.frame.size.height;
// if the local changes while in the background, we need to be notified so we can update the date
// format in the table view cells
//
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(localeChanged:)
name:NSCurrentLocaleDidChangeNotification
object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSCurrentLocaleDidChangeNotification
object:nil];
}
#pragma mark - Locale
/*! Responds to region format or locale changes.
*/
- (void)localeChanged:(NSNotification *)notif
{
// the user changed the locale (region format) in Settings, so we are notified here to
// update the date format in the table view cells
//
[self.tableView reloadData];
}
#pragma mark - Utilities
/*! Returns the major version of iOS, (i.e. for iOS 6.1.3 it returns 6)
*/
NSUInteger DeviceSystemMajorVersion()
{
static NSUInteger _deviceSystemMajorVersion = -1;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue];
});
return _deviceSystemMajorVersion;
}
#define EMBEDDED_DATE_PICKER (DeviceSystemMajorVersion() >= 7)
/*! Determines if the given indexPath has a cell below it with a UIDatePicker.
@param indexPath The indexPath to check if its cell has a UIDatePicker below it.
*/
- (BOOL)hasPickerForIndexPath:(NSIndexPath *)indexPath
{
BOOL hasDatePicker = NO;
NSInteger targetedRow = indexPath.row;
targetedRow++;
UITableViewCell *checkDatePickerCell =
[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:targetedRow inSection:2]];
UIDatePicker *checkDatePicker = (UIDatePicker *)[checkDatePickerCell viewWithTag:kDatePickerTag];
hasDatePicker = (checkDatePicker != nil);
return hasDatePicker;
}
/*! Updates the UIDatePicker's value to match with the date of the cell above it.
*/
- (void)updateDatePicker
{
if (self.datePickerIndexPath != nil)
{
UITableViewCell *associatedDatePickerCell = [self.tableView cellForRowAtIndexPath:self.datePickerIndexPath];
UIDatePicker *targetedDatePicker = (UIDatePicker *)[associatedDatePickerCell viewWithTag:kDatePickerTag];
if (targetedDatePicker != nil)
{
// we found a UIDatePicker in this cell, so update it's date value
//
NSDictionary *itemData = self.dataArray[self.datePickerIndexPath.row - 1];
[targetedDatePicker setDate:[itemData valueForKey:kDateKey] animated:NO];
}
}
}
/*! Determines if the UITableViewController has a UIDatePicker in any of its cells.
*/
- (BOOL)hasInlineDatePicker
{
return (self.datePickerIndexPath != nil);
}
/*! Determines if the given indexPath points to a cell that contains the UIDatePicker.
@param indexPath The indexPath to check if it represents a cell with the UIDatePicker.
*/
- (BOOL)indexPathHasPicker:(NSIndexPath *)indexPath
{
return ([self hasInlineDatePicker] && self.datePickerIndexPath.row == indexPath.row);
}
/*! Determines if the given indexPath points to a cell that contains the start/end dates.
@param indexPath The indexPath to check if it represents start/end date cell.
*/
- (BOOL)indexPathHasDate:(NSIndexPath *)indexPath
{
BOOL hasDate = NO;
if ((indexPath.row == kDateStartRow) ||
(indexPath.row == kDateEndRow || ([self hasInlineDatePicker] && (indexPath.row == kDateEndRow + 1))))
{
hasDate = YES;
}
return hasDate;
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([self indexPathHasPicker:indexPath] ? self.pickerCellRowHeight : self.tableView.rowHeight);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 2) {
if ([self hasInlineDatePicker])
{
// we have a date picker, so allow for it in the number of rows in this section
NSInteger numRows = self.dataArray.count;
return ++numRows;
}
return self.dataArray.count;
} else {
if (section == 0) {
return 2;
} else {
return 1;
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
if (indexPath.section <= 1) {
static NSString *CellIdentifier = @"Cell";
// Configure the cell...
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Hausaufgaben";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
cell.textLabel.text = @"Prüfung";
break;
default:
break;
}
} else {
UITextField *lblMainLabel = [[UITextField alloc]initWithFrame:CGRectMake(50, 9, 150, 25)];
lblMainLabel.tag = 42;
lblMainLabel.font = [UIFont fontWithName:@"Helvetica-Neue" size:15];
lblMainLabel.textColor = [UIColor blackColor];
lblMainLabel.placeholder = @"Beschreibung";
[cell.contentView addSubview:lblMainLabel];
cell.textLabel.text = @"Mat";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
} else {
NSString *cellID = kOtherCell;
if ([self indexPathHasPicker:indexPath])
{
// the indexPath is the one containing the inline date picker
cellID = kDatePickerID; // the current/opened date picker cell
}
else if ([self indexPathHasDate:indexPath])
{
// the indexPath is one that contains the date information
cellID = kDateCellID; // the start/end date cells
}
cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (indexPath.row == 0)
{
// we decide here that first cell in the table is not selectable (it's just an indicator)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// if we have a date picker open whose cell is above the cell we want to update,
// then we have one more cell than the model allows
//
NSInteger modelRow = indexPath.row;
if (self.datePickerIndexPath != nil && self.datePickerIndexPath.row < indexPath.row)
{
modelRow--;
}
NSDictionary *itemData = self.dataArray[modelRow];
// proceed to configure our cell
if ([cellID isEqualToString:kDateCellID])
{
// we have either start or end date cells, populate their date field
//
cell.textLabel.text = [itemData valueForKey:kTitleKey];
cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[itemData valueForKey:kDateKey]];
}
else if ([cellID isEqualToString:kOtherCell])
{
// this cell is a non-date cell, just assign it's text label
//
cell.textLabel.text = [itemData valueForKey:kTitleKey];
}
}
return cell;
}
/*! Adds or removes a UIDatePicker cell below the given indexPath.
@param indexPath The indexPath to reveal the UIDatePicker.
*/
- (void)toggleDatePickerForSelectedIndexPath:(NSIndexPath *)indexPath
{
[self.tableView beginUpdates];
NSArray *indexPaths = @[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:2]];
// check if 'indexPath' has an attached date picker below it
if ([self hasPickerForIndexPath:indexPath])
{
// found a picker below it, so remove it
[self.tableView deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
}
else
{
// didn't find a picker below it, so we should insert it
[self.tableView insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
/*! Reveals the date picker inline for the given indexPath, called by "didSelectRowAtIndexPath".
@param indexPath The indexPath to reveal the UIDatePicker.
*/
- (void)displayInlineDatePickerForRowAtIndexPath:(NSIndexPath *)indexPath
{
// display the date picker inline with the table content
[self.tableView beginUpdates];
BOOL before = NO; // indicates if the date picker is below "indexPath", help us determine which row to reveal
if ([self hasInlineDatePicker])
{
before = self.datePickerIndexPath.row < indexPath.row;
}
BOOL sameCellClicked = (self.datePickerIndexPath.row - 1 == indexPath.row);
// remove any date picker cell if it exists
if ([self hasInlineDatePicker])
{
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.datePickerIndexPath.row inSection:2]]
withRowAnimation:UITableViewRowAnimationFade];
self.datePickerIndexPath = nil;
}
if (!sameCellClicked)
{
// hide the old date picker and display the new one
NSInteger rowToReveal = (before ? indexPath.row - 1 : indexPath.row);
NSIndexPath *indexPathToReveal = [NSIndexPath indexPathForRow:rowToReveal inSection:2];
[self toggleDatePickerForSelectedIndexPath:indexPathToReveal];
self.datePickerIndexPath = [NSIndexPath indexPathForRow:indexPathToReveal.row + 1 inSection:2];
}
// always deselect the row containing the start or end date
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView endUpdates];
// inform our date picker of the current date to match the current cell
[self updateDatePicker];
}
/*! Reveals the UIDatePicker as an external slide-in view, iOS 6.1.x and earlier, called by "didSelectRowAtIndexPath".
@param indexPath The indexPath used to display the UIDatePicker.
*/
- (void)displayExternalDatePickerForRowAtIndexPath:(NSIndexPath *)indexPath
{
// first update the date picker's date value according to our model
NSDictionary *itemData = self.dataArray[indexPath.row];
[self.pickerView setDate:[itemData valueForKey:kDateKey] animated:YES];
// the date picker might already be showing, so don't add it to our view
if (self.pickerView.superview == nil)
{
CGRect startFrame = self.pickerView.frame;
CGRect endFrame = self.pickerView.frame;
// the start position is below the bottom of the visible frame
startFrame.origin.y = self.view.frame.size.height;
// the end position is slid up by the height of the view
endFrame.origin.y = startFrame.origin.y - endFrame.size.height;
self.pickerView.frame = startFrame;
[self.view addSubview:self.pickerView];
// animate the date picker into view
[UIView animateWithDuration:kPickerAnimationDuration animations: ^{ self.pickerView.frame = endFrame; }
completion:^(BOOL finished) {
// add the "Done" button to the nav bar
self.navigationItem.rightBarButtonItem = self.doneButton;
}];
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 0) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *cell2 = [[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
cell2.accessoryType = UITableViewCellAccessoryNone;
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}else {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *cell2 = [[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell2.accessoryType = UITableViewCellAccessoryNone;
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}else {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.reuseIdentifier == kDateCellID)
{
if (EMBEDDED_DATE_PICKER)
[self displayInlineDatePickerForRowAtIndexPath:indexPath];
else
[self displayExternalDatePickerForRowAtIndexPath:indexPath];
}
else
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
}
#pragma mark - Actions
/*! User chose to change the date by changing the values inside the UIDatePicker.
@param sender The sender for this action: UIDatePicker.
*/
- (IBAction)dateAction:(id)sender
{
NSIndexPath *targetedCellIndexPath = nil;
if ([self hasInlineDatePicker])
{
// inline date picker: update the cell's date "above" the date picker cell
//
targetedCellIndexPath = [NSIndexPath indexPathForRow:self.datePickerIndexPath.row - 1 inSection:2];
}
else
{
// external date picker: update the current "selected" cell's date
targetedCellIndexPath = [self.tableView indexPathForSelectedRow];
}
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:targetedCellIndexPath];
UIDatePicker *targetedDatePicker = sender;
// update our data model
NSMutableDictionary *itemData = self.dataArray[targetedCellIndexPath.row];
[itemData setValue:targetedDatePicker.date forKey:kDateKey];
// update the cell's date string
cell.detailTextLabel.text = [self.dateFormatter stringFromDate:targetedDatePicker.date];
}
/*! User chose to finish using the UIDatePicker by pressing the "Done" button, (used only for non-inline date picker), iOS 6.1.x or earlier
@param sender The sender for this action: The "Done" UIBarButtonItem
*/
- (IBAction)doneAction:(id)sender
{
CGRect pickerFrame = self.pickerView.frame;
pickerFrame.origin.y = self.view.frame.size.height;
// animate the date picker out of view
[UIView animateWithDuration:kPickerAnimationDuration animations: ^{ self.pickerView.frame = pickerFrame; }
completion:^(BOOL finished) {
[self.pickerView removeFromSuperview];
}];
// remove the "Done" button in the navigation bar
self.navigationItem.rightBarButtonItem = nil;
// deselect the current table cell
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
最佳答案
我为自己的应用修改了这个示例代码。我做了一件事,我修改了 ViewDidLoad 中的 MutableDictionary,将“其他单元格”文本更改为 @""。这使得它基本上是空白的,然后我在 Storyboard中明确了“其他细胞”的背景。这可能对你有用,也可能不适合你。 Apple 确实将这段代码抽象了很多。您可以将字典中的日期信息向上移动到位置 0,将标题向下移动到位置 2。然后将最后 3 个键的值更改为 @""。
这是我修改此示例代码所采用的方法。如果您这样开始,您也许可以到达您想去的地方。
这是我的应用程序的屏幕截图:
我不知道它是否允许我发布图片,我是新来这里发布的。但下面是我最终用来从您正在使用的相同示例代码制作表格 View 的代码。我遗漏了几个导入,可能应该遗漏更多不相关的代码,但我只是发布了整个 tableViewController。通过查看旧版本的 DateCell 示例代码,我明白了很多。它简单得多。但无论如何,希望这会有所帮助。
#define kPickerAnimationDuration 0.40 // duration for the animation to slide the date picker into view
#define kDatePickerTag 99 // view tag identifiying the date picker view
#define kTitleKey @"title" // key for obtaining the data source item's title
#define kDateKey @"date" // key for obtaining the data source item's date value
// keep track of which rows have date cells
#define kDateStartRow 2
#define kDateEndRow 3
static NSString *kDateCellID = @"dateCell"; // the cells with the start or end date
static NSString *kDatePickerID = @"datePicker"; // the cell containing the date picker
static NSString *kOtherCell = @"otherCell"; // the remaining cells at the end
#pragma mark -
@interface EditShiftDetailViewController ()
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
// keep track which indexPath points to the cell with UIDatePicker
@property (nonatomic, strong) NSIndexPath *datePickerIndexPath;
@property (assign) NSInteger pickerCellRowHeight;
@property (nonatomic, strong) IBOutlet UIDatePicker *pickerView;
// this button appears only when the date picker is shown (iOS 6.1.x or earlier)
@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton;
@end
#pragma mark -
@implementation EditShiftDetailViewController
/*! Primary view has been loaded for this view controller
*/
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:gestureRecognizer];
// setup our data source
NSMutableDictionary *itemOne = [@{ kTitleKey : @"Tap a cell to change its value:" } mutableCopy];
NSMutableDictionary *itemTwo = [@{ kTitleKey : @"Shift Name:" } mutableCopy];
NSMutableDictionary *itemThree = [@{ kTitleKey : @"Start Time",
kDateKey : self.currentShift.startTime } mutableCopy];
NSMutableDictionary *itemFour = [@{ kTitleKey : @"End Time",
kDateKey : self.currentShift.endTime } mutableCopy];
NSMutableDictionary *itemFive = [@{ kTitleKey : @"" } mutableCopy] ;
self.dataArray = @[itemOne, itemTwo, itemThree, itemFour, itemFive];
self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setDateStyle:NSDateFormatterNoStyle]; // Don't show date
[self.dateFormatter setTimeStyle:NSDateFormatterShortStyle]; // show short-style time format
// obtain the picker view cell's height, works because the cell was pre-defined in our storyboard
UITableViewCell *pickerViewCellToCheck = [self.tableView dequeueReusableCellWithIdentifier:kDatePickerID];
self.pickerCellRowHeight = pickerViewCellToCheck.frame.size.height;
//self.shiftNameField.text = self.currentShift.shiftName;
self.title = [NSString stringWithFormat:@"%@, %@", self.currentDay.name, self.currentShift.shiftName];
// if the local changes while in the background, we need to be notified so we can update the date
// format in the table view cells
//
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(localeChanged:)
name:NSCurrentLocaleDidChangeNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.currentShift.startTime = [self.dataArray[2] valueForKey:kDateKey];
self.currentShift.endTime = [self.dataArray[3] valueForKey:kDateKey];
[[MyManager sharedManager] saveChanges];
}
- (void) hideKeyboard
{
[self.view endEditing:YES];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSCurrentLocaleDidChangeNotification
object:nil];
}
#pragma mark - Locale
/*! Responds to region format or locale changes.
*/
- (void)localeChanged:(NSNotification *)notif
{
// the user changed the locale (region format) in Settings, so we are notified here to
// update the date format in the table view cells
//
[self.tableView reloadData];
}
#pragma mark - Utilities
/*! Returns the major version of iOS, (i.e. for iOS 6.1.3 it returns 6)
*/
NSUInteger DeviceSystemMajorVersion()
{
static NSUInteger _deviceSystemMajorVersion = -1;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue];
});
return _deviceSystemMajorVersion;
}
#define EMBEDDED_DATE_PICKER (DeviceSystemMajorVersion() >= 7)
/*! Determines if the given indexPath has a cell below it with a UIDatePicker.
@param indexPath The indexPath to check if its cell has a UIDatePicker below it.
*/
- (BOOL)hasPickerForIndexPath:(NSIndexPath *)indexPath
{
BOOL hasDatePicker = NO;
NSInteger targetedRow = indexPath.row;
targetedRow++;
UITableViewCell *checkDatePickerCell =
[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:targetedRow inSection:0]];
UIDatePicker *checkDatePicker = (UIDatePicker *)[checkDatePickerCell viewWithTag:kDatePickerTag];
hasDatePicker = (checkDatePicker != nil);
return hasDatePicker;
}
/*! Updates the UIDatePicker's value to match with the date of the cell above it.
*/
- (void)updateDatePicker
{
if (self.datePickerIndexPath != nil)
{
UITableViewCell *associatedDatePickerCell = [self.tableView cellForRowAtIndexPath:self.datePickerIndexPath];
UIDatePicker *targetedDatePicker = (UIDatePicker *)[associatedDatePickerCell viewWithTag:kDatePickerTag];
if (targetedDatePicker != nil)
{
// we found a UIDatePicker in this cell, so update it's date value
//
NSDictionary *itemData = self.dataArray[self.datePickerIndexPath.row - 1];
[targetedDatePicker setDate:[itemData valueForKey:kDateKey] animated:NO];
}
}
}
/*! Determines if the UITableViewController has a UIDatePicker in any of its cells.
*/
- (BOOL)hasInlineDatePicker
{
return (self.datePickerIndexPath != nil);
}
/*! Determines if the given indexPath points to a cell that contains the UIDatePicker.
@param indexPath The indexPath to check if it represents a cell with the UIDatePicker.
*/
- (BOOL)indexPathHasPicker:(NSIndexPath *)indexPath
{
return ([self hasInlineDatePicker] && self.datePickerIndexPath.row == indexPath.row);
}
/*! Determines if the given indexPath points to a cell that contains the start/end dates.
@param indexPath The indexPath to check if it represents start/end date cell.
*/
- (BOOL)indexPathHasShiftName:(NSIndexPath *)indexPath
{
return (indexPath.row == 1);
}
- (BOOL)indexPathHasDate:(NSIndexPath *)indexPath
{
BOOL hasDate = NO;
if ((indexPath.row == kDateStartRow) ||
(indexPath.row == kDateEndRow || ([self hasInlineDatePicker] && (indexPath.row == kDateEndRow + 1))))
{
hasDate = YES;
}
return hasDate;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.currentShift.shiftName = textField.text;
self.title = [NSString stringWithFormat:@"%@, %@", self.currentDay.name, self.currentShift.shiftName];
[self.tableView reloadData];
return YES;
}
#pragma mark - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([self indexPathHasPicker:indexPath] ? self.pickerCellRowHeight : self.tableView.rowHeight);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self hasInlineDatePicker])
{
// we have a date picker, so allow for it in the number of rows in this section
NSInteger numRows = self.dataArray.count;
return ++numRows;
}
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSString *cellID = kOtherCell;
if ([self indexPathHasPicker:indexPath])
{
// the indexPath is the one containing the inline date picker
cellID = kDatePickerID; // the current/opened date picker cell
}
else if ([self indexPathHasDate:indexPath])
{
// the indexPath is one that contains the date information
cellID = kDateCellID; // the start/end date cells
}
else if ([self indexPathHasShiftName:indexPath])
{
cellID = @"shiftNameCell";
}
cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (indexPath.row == 0)
{
// we decide here that first cell in the table is not selectable (it's just an indicator)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// if we have a date picker open whose cell is above the cell we want to update,
// then we have one more cell than the model allows
//
NSInteger modelRow = indexPath.row;
if (self.datePickerIndexPath != nil && self.datePickerIndexPath.row < indexPath.row)
{
modelRow--;
}
NSDictionary *itemData = self.dataArray[modelRow];
// proceed to configure our cell
if ([cellID isEqualToString:kDateCellID])
{
// we have either start or end date cells, populate their date field
//
cell.textLabel.text = [itemData valueForKey:kTitleKey];
cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[itemData valueForKey:kDateKey]];
}
else if ([cellID isEqualToString:@"shiftNameCell"])
{
UITextField *txtField = (UITextField *)[cell viewWithTag:8];
txtField.delegate = self;
txtField.enablesReturnKeyAutomatically = YES;
txtField.returnKeyType = UIReturnKeyDone;
txtField.text = self.currentShift.shiftName;
}
else if ([cellID isEqualToString:kOtherCell])
{
// this cell is a non-date cell, just assign it's text label
//
/*if (indexPath.row == 4) {
NSTimeInterval timeInterval = [self.currentShift.endTime timeIntervalSinceDate:self.currentShift.startTime];
NSString *cellText = [self stringFromTimeInterval:timeInterval];
cell.textLabel.text = [NSString stringWithFormat:@"Shift Length %@ hours", cellText];
} else { */
cell.textLabel.text = [itemData valueForKey:kTitleKey];
// }
}
return cell;
}
- (NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
NSInteger ti = (NSInteger)interval;
NSInteger minutes = (ti / 60) % 60;
NSInteger hour = (ti / 3600);
return [NSString stringWithFormat:@"%i:%02i", hour, minutes];
}
/*! Adds or removes a UIDatePicker cell below the given indexPath.
@param indexPath The indexPath to reveal the UIDatePicker.
*/
- (void)toggleDatePickerForSelectedIndexPath:(NSIndexPath *)indexPath
{
[self.tableView beginUpdates];
NSArray *indexPaths = @[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]];
// check if 'indexPath' has an attached date picker below it
if ([self hasPickerForIndexPath:indexPath])
{
// found a picker below it, so remove it
[self.tableView deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
}
else
{
// didn't find a picker below it, so we should insert it
[self.tableView insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
/*! Reveals the date picker inline for the given indexPath, called by "didSelectRowAtIndexPath".
@param indexPath The indexPath to reveal the UIDatePicker.
*/
- (void)displayInlineDatePickerForRowAtIndexPath:(NSIndexPath *)indexPath
{
// display the date picker inline with the table content
[self.tableView beginUpdates];
BOOL before = NO; // indicates if the date picker is below "indexPath", help us determine which row to reveal
if ([self hasInlineDatePicker])
{
before = self.datePickerIndexPath.row < indexPath.row;
}
BOOL sameCellClicked = (self.datePickerIndexPath.row - 1 == indexPath.row);
// remove any date picker cell if it exists
if ([self hasInlineDatePicker])
{
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.datePickerIndexPath.row inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
self.datePickerIndexPath = nil;
}
if (!sameCellClicked)
{
// hide the old date picker and display the new one
NSInteger rowToReveal = (before ? indexPath.row - 1 : indexPath.row);
NSIndexPath *indexPathToReveal = [NSIndexPath indexPathForRow:rowToReveal inSection:0];
[self toggleDatePickerForSelectedIndexPath:indexPathToReveal];
self.datePickerIndexPath = [NSIndexPath indexPathForRow:indexPathToReveal.row + 1 inSection:0];
}
// always deselect the row containing the start or end date
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView endUpdates];
// inform our date picker of the current date to match the current cell
[self updateDatePicker];
}
/*! Reveals the UIDatePicker as an external slide-in view, iOS 6.1.x and earlier, called by "didSelectRowAtIndexPath".
@param indexPath The indexPath used to display the UIDatePicker.
*/
- (void)displayExternalDatePickerForRowAtIndexPath:(NSIndexPath *)indexPath
{
// first update the date picker's date value according to our model
NSDictionary *itemData = self.dataArray[indexPath.row];
[self.pickerView setDatePickerMode:UIDatePickerModeTime];
[self.pickerView setMinuteInterval:5];
[self.pickerView setDate:[itemData valueForKey:kDateKey] animated:YES];
// the date picker might already be showing, so don't add it to our view
if (self.pickerView.superview == nil)
{
CGRect startFrame = self.pickerView.frame;
CGRect endFrame = self.pickerView.frame;
// the start position is below the bottom of the visible frame
startFrame.origin.y = self.view.frame.size.height;
// the end position is slid up by the height of the view
endFrame.origin.y = startFrame.origin.y - endFrame.size.height;
self.pickerView.frame = startFrame;
[self.view addSubview:self.pickerView];
// animate the date picker into view
[UIView animateWithDuration:kPickerAnimationDuration animations: ^{ self.pickerView.frame = endFrame; }
completion:^(BOOL finished) {
// add the "Done" button to the nav bar
self.navigationItem.rightBarButtonItem = self.doneButton;
}];
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.reuseIdentifier == kDateCellID)
{
if (EMBEDDED_DATE_PICKER)
[self displayInlineDatePickerForRowAtIndexPath:indexPath];
else
[self displayExternalDatePickerForRowAtIndexPath:indexPath];
}
else
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
#pragma mark - Actions
/*! User chose to change the date by changing the values inside the UIDatePicker.
@param sender The sender for this action: UIDatePicker.
*/
- (IBAction)dateAction:(id)sender
{
NSIndexPath *targetedCellIndexPath = nil;
if ([self hasInlineDatePicker])
{
// inline date picker: update the cell's date "above" the date picker cell
//
targetedCellIndexPath = [NSIndexPath indexPathForRow:self.datePickerIndexPath.row - 1 inSection:0];
}
else
{
// external date picker: update the current "selected" cell's date
targetedCellIndexPath = [self.tableView indexPathForSelectedRow];
}
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:targetedCellIndexPath];
UIDatePicker *targetedDatePicker = sender;
// update our data model
NSMutableDictionary *itemData = self.dataArray[targetedCellIndexPath.row];
[itemData setValue:targetedDatePicker.date forKey:kDateKey];
// update the cell's date string
cell.detailTextLabel.text = [self.dateFormatter stringFromDate:targetedDatePicker.date];
[[MyManager sharedManager] saveChanges];
[self.tableView reloadData];
}
/*! User chose to finish using the UIDatePicker by pressing the "Done" button, (used only for non-inline date picker), iOS 6.1.x or earlier
@param sender The sender for this action: The "Done" UIBarButtonItem
*/
- (IBAction)doneAction:(id)sender
{
CGRect pickerFrame = self.pickerView.frame;
pickerFrame.origin.y = self.view.frame.size.height;
// animate the date picker out of view
[UIView animateWithDuration:kPickerAnimationDuration animations: ^{ self.pickerView.frame = pickerFrame; }
completion:^(BOOL finished) {
[self.pickerView removeFromSuperview];
}];
// remove the "Done" button in the navigation bar
self.navigationItem.rightBarButtonItem = nil;
// deselect the current table cell
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
关于iphone - 从 Apple DateCell 模板中删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19365969/
一直以来中国版Apple Watch心不支持心电图功能,不过近日Apple Watch心电图终于在国行版上线了!那么Apple Watch心国内版心电图要如何使用呢?下面一起来看看吧! App
我正在尝试将“使用 Apple 登录”添加到我现有的 App ID。检查启用它的选项后,显示以下弹出窗口:带有此消息: If you're enabling an App ID for the fir
我有一个并发症,可能需要每 5 分钟更新一次。这很容易总结为每天 120 次更新。有没有办法只在用户唤醒 watch 时更新? 最佳答案 我认为您的问题的答案是否,目前没有办法只在用户唤醒 watch
我们正在测试新 Sign in with Apple我们的应用程序的功能,并且在初始请求时,我们会提供用户的全名和电子邮件地址(如果用户启用了这些选项)。 但是在随后的请求中,此数据不仅提供 iden
在我的苹果 watch 扩展中,我想使用长按手势功能。是否有任何 api 等效于 UILongPressGestureRecognizer。我的要求是,在 watch 扩展上,我有表格想要长按单元格,
有没有办法以编程方式显示苹果 map 中多个点之间的路线,如谷歌地图? 最佳答案 正如 MKMapItem 文档所述: If you specify the MKLaunchOptionsDirect
我一直在互联网上关注很多教程来学习如何设置并发症。按预期设置并发症我没有问题。 直到初始时间线条目过期。 12 小时后,我不知道如何更新它以保持并发症的存在。我将在下面分享我拥有的所有内容,希望有人可
我看到一本书的描述...... 书上说 /^Apple/ 会匹配字符串开头有一个 Apple 的字符串。所以它将匹配 Apple Apple1 AppleApple AppleABC ...... 书
众所周知,您可以禁止从允许接收 Apple 通知的应用程序接收通知。但是有谁知道禁用是在本地进行的(忽略 Apple 发送到应用程序的通知),还是 Apple 停止从它的服务器向您发送通知? 最佳答案
我有一个 Apple id,我正在构建一个使用 Apple 推送通知服务的应用程序,但我对此有点困惑。 Apple 执行此过程是否收费?它可以在安装了我的应用程序的特定数量的设备上运行是否有任何限制?
我正在制作一个音频播放器应用。 在苹果的音乐应用中,如果音乐专辑或播客没有插图,则显示音符图像或播客图标图像而不是插图。 我想做同样的事情。 我可以在我的应用程序中使用苹果音乐应用程序中的图像吗? 苹
我有一个自定义框架,我正在归档以在另一个项目中使用。更新到 Xcode11 后,我在使用该框架的项目中收到以下错误。 找不到目标“x86_64-apple-ios-simulator”的模块“MyCu
我有一个在 iOS 上运行良好的应用程序,但是当使用催化剂运行时,如果我在 macOS 上滑动到另一个虚拟桌面,然后再返回大约 10 次,它会间歇性地崩溃。它主要发生在 UICollectionVie
我正在使用 Xcode 开发 Apple Watch 应用程序。我想在屏幕的左上角放置一些文本,与列出时间的位置相邻。 当我将标签向上拖动到屏幕的一部分时,它会自动向下对齐。 我看到大多数 Apple
我似乎找不到在哪里设置我的 Apple Watch 应用程序的产品名称。我确实看到了产品名称选项,但更新它没有任何作用。也看不到文档中的任何内容 最佳答案 为了让您的应用程序名称在 iPhone 上的
问题:如何在我的服务器产品的安装程序中安全地包含推送通知所需的 SSL 证书? 背景:Apple 推送通知要求客户端 SSL 证书位于向 Apple 发出调用的服务器上。 我的产品采用传统的客户端/服
我已经在我的网站上实现了 Sign In with Apple。但问题是它只适用于我开发者的 Apple ID。 我尝试在同一环境中使用我的个人 Apple ID,并且登录过程也运行良好。 但是,当真
我的苹果触摸图标中的白色背景变黑了??我的白色背景不透明。该图标有一个白色三角形、红色圆圈和黑色文本。我唯一能辨认出来的是白色三角形和红色圆圈。知道是什么导致了这种情况以及如何使图标保持白色背景吗?
我正在考虑制作一个使用加速度计的 watchOS2 应用程序。如果应用程序在后台运行,它是否仍然能够接收来自加速度计或 CMMotionManager 的输入? 最佳答案 只有当 watchOS2 应
我想切换 Apple App Loader 使用的 Apple ID。 我找不到更改应用程序本身使用的帐户的方法。谷歌搜索没有带来任何有用的信息。当我启动加载程序应用程序时,它给我一个错误:“...您
我是一名优秀的程序员,十分优秀!