【发布时间】:2012-08-14 19:18:00
【问题描述】:
- 我的应用有 UITableViewController 和 DetailViewController。
- 具有多单元格选择和详细附件按钮的 UITableViewController。
- 如果用户选择多个单元格并点击 TOTAL 按钮警报消息,则会显示课程总价。
- 如果用户点击 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;
DetailViewController.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 以正常方式运行良好。
【问题讨论】:
-
那么究竟是什么不起作用?
-
当用户点击单元格时,他/她将导航到 DetailView。但这不是我想要的。我只想使用 Detail 附件按钮进行导航和单元格选择来进行搭配。希望清楚:)
-
我想用这个
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath,但是prepareForSegue中的indexPathForSelectedRow阻止了它!!
标签: ios xcode ios5 xcode4 xcode4.3