- 我的应用程序有 UITableViewController 和 DetailViewController。
- UITableViewController 具有多单元选择和详细附件按钮。
- 如果用户选择多个单元格并点击 TOTAL Button Alert 消息,则会显示课程总价格。
- 如果用户点击 Detail 附件按钮,它会转到 DetailViewController。
好的,现在我的问题是我怎么能用我的代码做到这一点:
UITableViewController.m
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[object valueForKey:@"courseCode"] description];
cell.detailTextLabel.text = [[object valueForKey:@"courseName"] description];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
}
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([[[object valueForKey:@"creditHours"] description]isEqualToString: @""]) {
}
//some thing doing if the user deselect the cell..
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([[[object valueForKey:@"creditHours"] description]isEqualToString: @""]) {
}
//some thing doing if the user deselect the cell..
}
- (IBAction)doneButton:(id)sender {
given the total...
}
DetailViewController.h
@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UITextField *courseCodeLabel;
@property (weak, nonatomic) IBOutlet UITextField *courseNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *creditHoursLabel;
@property (weak, nonatomic) IBOutlet UITextField *preRequisitesLabel;
@property (weak, nonatomic) IBOutlet UITextField *coursePriceLabel;
@property (weak, nonatomic) IBOutlet UITextField *courseEPPLabel;
@property (weak, nonatomic) IBOutlet UITextView *courseDetailLabel;
细节视图控制器.m
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
@synthesize detailItem = _detailItem;
@synthesize courseCodeLabel = _courseCodeLabel;
@synthesize courseNameLabel = _courseNameLabel;
@synthesize creditHoursLabel = _creditHoursLabel;
@synthesize preRequisitesLabel = _preRequisitesLabel;
@synthesize coursePriceLabel = _coursePriceLabel;
@synthesize courseEPPLabel = _courseEPPLabel;
@synthesize courseDetailLabel = _courseDetailLabel;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
[self configureView];
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.courseCodeLabel.text = [[self.detailItem valueForKey:@"courseCode"] description];
self.courseNameLabel.text = [[self.detailItem valueForKey:@"courseName"] description];
self.creditHoursLabel.text = [[self.detailItem valueForKey:@"creditHours"] description];
self.preRequisitesLabel.text = [[self.detailItem valueForKey:@"preRequisites"] description];
self.coursePriceLabel.text = [[self.detailItem valueForKey:@"coursePrice"] description];
self.courseEPPLabel.text = [[self.detailItem valueForKey:@"courseEPP"] description];
self.courseDetailLabel.text = [[self.detailItem valueForKey:@"courseDetails"] description];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
请注意,segue 以正常方式运行良好。