0

我无法让我的 plist 显示在我的应用程序上。我需要使用 TableView 而不是 TableView 控制器。我不确定我第一次尝试时是否遵循了错误的代码。请看一下。米。文件是:

#import "ECSlidingViewController.h"
#import "NewsFeedViewController.h"
#import "BuySharesViewController.h"
#import "SellSharesViewController.h"
#import "FinancesViewController.h"
#import "CurrentHoldingsViewController.h"
#import "TradingHistoryViewController.h"
#import "LeaderboardViewController.h"
#import "HowToPlayViewController.h"
#import "MenuViewController.h"
#import "InitViewController.h"

@interface SellSharesViewController ()

{
    BOOL isSearching;
}

@property (nonatomic, readonly) NSDate *CurrentDate;
@property (nonatomic, strong) NSDictionary *shares;
@property (nonatomic, strong) NSArray *shareValue;
@property (nonatomic, strong) NSArray *number;
@property (nonatomic, strong) NSArray *shareName;

- (void)resetSearch;
- (void)handleSearchForTerm:(NSString *)searchTerm;

@end


@implementation SellSharesViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    isSearching = NO;
    NSString *path=[[NSBundle mainBundle] pathForResource:@"shares" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.allNames = dict;
    //NSArray *array = [[self.names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    //self.keys = (NSMutableArray *)array;

    [self resetSearch];
    [self.table reloadData];
    [self.table setContentOffset:CGPointMake(0.0, 44.0) animated:NO];


    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return ([self.keys count] > 0) ? [self.keys count] : 1 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    if ([self.keys count] == 0)
        return 0 ;

    NSString *key = [self.keys objectAtIndex:section];
    NSArray *nameSection = [self.names objectForKey:key];
    return nameSection.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [self.keys objectAtIndex:section];
    NSArray *nameSection = [self.names objectForKey:key];

    cell.textLabel.text = [nameSection objectAtIndex:row];

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  {

    if ([self.keys count] == 0)
        return nil;


    NSString *key = [self.keys objectAtIndex:section];
    if (key == UITableViewIndexSearch)
        return nil;

    return key;
}



/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    if (isSearching)
        return nil;

    return self.keys;
}



#pragma mark -
#pragma mark Custom Methods

- (void) resetSearch {
    NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy];
    self.names = allNamesCopy;

    NSMutableArray *keyArray = [[NSMutableArray alloc] init];
    [keyArray addObjectsFromArray:[[self.allNames allKeys]
                                   sortedArrayUsingSelector:@selector(compare:)]];
    [keyArray insertObject:UITableViewIndexSearch atIndex:0];
    self.keys = keyArray;
}

- (void)handleSearchForTerm:(NSString *)searchTerm {
    NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
    [self resetSearch];

    for (NSString *key in self.keys) {
        NSMutableArray *array = [self.names valueForKey:key];
        NSMutableArray *toRemove = [[NSMutableArray alloc] init];
        for (NSString *name in array ) {
            if ([name rangeOfString:searchTerm
                            options:NSCaseInsensitiveSearch].location == NSNotFound)
                [toRemove addObject:name];
        }

        if ([array count] == [toRemove count])
            [sectionsToRemove addObject:key];

        [array removeObjectsInArray:toRemove];
    }
    [self.keys removeObjectsInArray:sectionsToRemove];
    [self.table reloadData];
}



#pragma mark -
#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.search resignFirstResponder];
    isSearching = NO;
    self.search.text = @"";
    [self.table reloadData];
    return indexPath;
}

#pragma mark -
#pragma mark Search Bar Delegate Methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *searchTerm = [searchBar text];
    [self handleSearchForTerm:searchTerm];
}

- (void)searchBar:(UISearchBar *)searchBar
    textDidChange:(NSString *)searchTerm {
    if ([searchTerm length] == 0) {
        [self resetSearch];
        [self.table reloadData];
        return;
    }
    [self handleSearchForTerm:searchTerm];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    isSearching = NO;
    self.search.text = @"" ;
    [self resetSearch];
    [self.table reloadData];
    [searchBar resignFirstResponder];
}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    isSearching = YES;
    [self.table reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)titleForHeaderInSection
               atIndex:(NSInteger)index {
    NSString *key = [self.keys objectAtIndex:index];
    if (key == UITableViewIndexSearch) {
        [tableView setContentOffset:CGPointZero animated:NO];
        return NSNotFound;
    }
    else {
        return index;
    }
}




@end

h.file 是:

#import <UIKit/UIKit.h>

@interface SellSharesViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>


@property (weak, nonatomic) IBOutlet UITableView *sellShares;
@property (weak, nonatomic) IBOutlet UILabel *rowSelectedDisplay;

@property (weak, nonatomic) IBOutlet UIButton *removeShare;



@property (strong, nonatomic) UIButton *menuBtn;

-(IBAction)removeShareButton:(id)sender;

@end

随意要求任何其他可能有帮助的东西。谢谢。

4

2 回答 2

0

将 uitableview 属性更改为强而不是弱。我这样做是为了有一个指向 tableview 的强指针,所以它不会被删除。您还需要将 uitableview 的委托和数据源连接到情节提要中的 uiviewcontroller。所以协议方法将在你的 uiviewcontroller 中被调用。如果它们未连接,则不会调用协议方法,因此表视图中不会显示任何内容。

如果你有一个字典数组,fornumberofrows 返回 [myarray count];

连接故事板中的数据源和委托被忽略了很多次,所以先检查一下。我希望这有帮助!

于 2013-04-22T23:47:40.413 回答
0

您是否在 XIB 中设置了 Delegate 和 Datasource? 在此处输入图像描述

您需要检查的其他事项是,在下面的方法中放置断点并检查它是否正在触发

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

确保在前两种方法中没有返回零。

如果前两种方法中的任何一种都没有触发,那么这意味着您没有在 XIB 中添加数据源

于 2013-04-23T07:11:13.447 回答