- 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/
我试图要求 test/unit 的新版本(即不是与 ruby 捆绑的版本) .根据 instructions我用 gem i test-unit 安装它.但是现在当我需要 test/unit我似乎
简短版本是: 我有一个 systemd 单元,我想在调用时检查脚本的返回代码: systemctl status service.service 长版本:我有一个 lsb init 脚本正是这样做的,
我正在使用反射动态创建一个类的实例,这工作正常,除非尝试通过单元测试执行此操作 - 我使用的是 MS 测试框架。 我收到熟悉的错误:“无法加载文件或程序集‘Assy’或其依赖项之一。系统找不到指定的文
我想知道测试网站“界面功能”的最佳实践是什么。 我对哪些方法可用于测试界面和动态生成的内容感兴趣。特别是,我很难弄清楚是否可以为需要用户交互的操作创建自动化测试,或者这是否只是浪费时间,我应该让一些
我有一个简单的 Python 服务,其中有一个无限执行某些操作的循环。在各种信号上,sys.exit(0) 被调用,这导致 SystemExit 被引发,然后如果可以的话应该进行一些清理。 在测试中,
我正在使用 OpenCV 2.4.2 这是 OpenCV 文档中的引文 C++: void HoughLinesP(InputArray image, OutputArray lines, doubl
忙于 C# 中的自动化测试用例,我们需要在每次测试后恢复数据库快照。问题是,当运行多个测试时它会失败,因为“其他用户正在使用数据库时无法更改数据库状态。” 我们使用 SqlConnection.Cle
我阅读了 C# 规范并用谷歌搜索了它,但一无所获。 我 99% 肯定 C# 中没有像单元命名空间指令这样的功能,但问题是:为什么?是否有惯用或技术原因? 这很方便,尤其是当我们的大部分文件都由单个命名
我目前正在尝试向我的应用程序(一个非常老的项目......评论说 iOS 2.0)添加单元测试(精确的应用程序测试)并且总是偶然发现 undefined symbols for architectur
我正在使用Delphi 7,并且有一个新单元要在我的项目中使用。我已经编译了新的。当我尝试通过将其添加到uses子句在项目中使用此单元时,出现错误,提示未找到.dcu文件。我还尝试将文件的完整路径放在
场景:我需要编写一个复杂的nHibernate查询,该查询将返回预计的DTO,但是我想使用TDD方法。该方法如下所示: public PrintDTO GetUsersForPrinting(int
您可以通过运行以下命令在事件 html 设置中显示 Jupyter 笔记本: $ jupyter nbconvert untitled.ipynb --to slides --post serve 有
如何在一个网站上拥有多个 AdSense 单元? Google 提供的唯一代码是按单位计算的。 (adsbygoogle = window.adsbygoogle || []).push({})
我刚刚开始为大量代码编写测试。有很多类依赖于文件系统,即读取 CSV 文件、读/写配置文件等。 当前测试文件存储在项目(这是一个 Maven2 项目)的 test 目录中,但由于多种原因该目录并不总是
我对 TDD 还很陌生,在单元测试方面也不是很老练,所以才有这个问题。我有这个用 PHP 编写的遗留函数 function foo(){ x = bar(); y = baz();
我创建了一个程序,在 Swing 窗口的一侧显示结果过滤选项,但默认情况下它们水平相邻显示,这浪费了我在 BorderLayout 的西侧分配的空间。我可以在构造函数或添加语句中传递任何内容来将它们堆
标题不好的借口:如果有人能更好地描述它,请做。 我有一个 WeakList类,它“基本上”是一个 List> (虽然不是字面意义上的派生自列表,但它应该对用户完全透明)。 现在的基本思想是“如果引用的
我正在尝试在 UITableView 上添加两个原型(prototype)单元。但是,我不知道如何验证是否能够为每个原型(prototype)“返回”正确的单元格。你们能帮我一下吗? func ta
我正在使用 CloudKit 作为数据库创建一个简单的待办事项列表应用程序。目前我可以添加和删除对象,但对编辑对象感到困惑。 编辑项目 Controller protocol EditItemCont
我正在针对以下任务训练 RNN:给定一个包含 30 个单词的序列,然后将该序列分类为二进制类。 在我的网络中拥有超过 30 个单元(LSTM、GRU 或普通 RNN)有好处吗?我在网上看到过很多例子,
我是一名优秀的程序员,十分优秀!