显示 plist 的 TableView 代码不起作用,是因为它必须在 UITableView 控制器上吗? (Xcode)
Posted
技术标签:
【中文标题】显示 plist 的 TableView 代码不起作用,是因为它必须在 UITableView 控制器上吗? (Xcode)【英文标题】:TableView code to display plist is not working, Is it because it has to be on a UITableView Controller? (Xcode) 【发布时间】:2013-04-22 16:49:13 【问题描述】:我无法让我的 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
请随时提出其他可能有帮助的问题。谢谢。
【问题讨论】:
【参考方案1】:将 uitableview 属性更改为强而不是弱。我这样做是为了有一个指向 tableview 的强指针,所以它不会被删除。您还需要将 uitableview 的委托和数据源连接到情节提要中的 uiviewcontroller。所以协议方法将在你的 uiviewcontroller 中被调用。如果它们未连接,则不会调用协议方法,因此表视图中不会显示任何内容。
如果你有一个字典数组,fornumberofrows 返回 [myarray count];
在故事板中连接数据源和委托被忽略了很多次,所以先检查一下。我希望这会有所帮助!
【讨论】:
非常好,它奏效了。我没有将委托和数据源连接到控制器。非常感谢。下一步是将选择的“共享”添加到不同控制器上的另一个 plist 以保留历史记录。有什么想法请指教!【参考方案2】:您是否在 XIB 中设置了 Delegate 和 Datasource?
您需要检查的其他事项是, 在下面的方法中放置断点并检查它是否正在触发
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
确保在前两种方法中没有返回零。
如果前两种方法中的任何一种都没有触发,则表示您没有在 XIB 中添加数据源
【讨论】:
以上是关于显示 plist 的 TableView 代码不起作用,是因为它必须在 UITableView 控制器上吗? (Xcode)的主要内容,如果未能解决你的问题,请参考以下文章
为啥在我滚动显示来自 plist 的数据的 tableview 后我的应用程序崩溃?
iOS项目开发实战——学会使用TableView列表控件plist读取与Section显示
SWIFT 属性列表:从 TableView 到 DetailView plist