Einfache App die eine TableView (plist) und eine SearchBar hat (Anfänger)

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Einfache App die eine TableView (plist) und eine SearchBar hat (Anfänger)

    Hi,
    ich will eine kleine App schreiben die eine TableView hat und Daten aus einer plist lädt .
    Das hab ich soweit auch durch diverse Videos hinbekommen aber sobald ich eine Searchbar anlegen will killt sich die App.

    CellViewController.h & CellViewController.m
    Spoiler anzeigen
    #import <UIKit/UIKit.h>

    #import "CustomCell.h"

    @interface CellViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>

    @property (strong, nonatomic) IBOutlet UITableView *myTableView;

    @property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

    @end


    ---------------------------------------------------------------------------------------------------------------------------------------------------
    CellViewController.m



    #import "CellViewController.h"

    #import "CustomCell.h"

    @interface CellViewController ()

    {

    NSArray *Title;

    NSArray *SubTitle;

    NSArray *QuestionArray;

    }

    @property (nonatomic, copy) NSMutableArray *filtered;

    @property (nonatomic, strong)UISearchDisplayController *searchController;

    @end

    @implementation CellViewController

    @synthesize filtered, searchController;

    - (id)initWithStyle:(UITableViewStyle)style

    {

    self = [super initWithStyle:style];
    if (self) {
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    UITableView *tableView = (id)[self.view viewWithTag:1];

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

    filtered = [[NSMutableArray alloc]init];

    searchController = [[UISearchDisplayController alloc]init];

    searchController.searchResultsDataSource = self;

    self.myTableView.dataSource = self;

    self.myTableView.delegate = self;

    NSString *pfad = [[NSBundle mainBundle] pathForResource:@"Que" ofType:@"plist"];

    QuestionArray = [NSArray arrayWithContentsOfFile:pfad];
    }

    - (void)didReceiveMemoryWarning

    {

    [super didReceiveMemoryWarning];

    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {
    return 1;
    }

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

    {
    if (tableView.tag == 1) {

    return [QuestionArray count];

    }
    else {
    return [filtered count];
    }

    }

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

    CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (tableView.tag == 1) {

    Cell.Title.text = [[QuestionArray objectAtIndex:indexPath.row] objectForKey:@"Name"];

    Cell.SubTitle.text = [[QuestionArray objectAtIndex:indexPath.row] objectForKey:@"Number"];

    }

    else {

    Cell.Title.text = filtered[indexPath.row];

    }
    //Cell.Title.text = [[QuestionArray objectAtIndex:indexPath.row] objectForKey:@"Name"];
    //Cell.SubTitle.text = [[QuestionArray objectAtIndex:indexPath.row] objectForKey:@"Number"];

    return Cell;

    }
    @end


    Eigentlich eine einfache kiste (TableView / Pilst / searchBar) aber ich scheitere wohl schon hier dran.

    Ich will eine App haben in der ich nach Fragen/Antworten suchen kann. Und die Plist dann später einfach erweitern.

    Wie kann man hier am besten seinen Code teilen bzw sein Projekt ?

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von Robert Schmidt ()

  • Ohne Crash-Log und Stack-Trace lässt sich da nur raten.

    Ich tippe aber mal auf die folgende Zeile:

    Quellcode

    1. searchController = [[UISearchDisplayController alloc]init];

    Hier musst Du den Setter, also self.searchController verwenden!

    Merke: Beim Ansprechen von Properties, insbesondere bei der Wertzuweisung an ein Property, immer den Setter, also self.xxx verwenden!

    Weiterhin ist die Initialisierung des UISearchDisplayController falsch. Diese muss über initWithSearchBar:contentsController: erfolgen.
  • Log
    Spoiler anzeigen
    2014-06-26 14:45:19.305 TableViewWithPlist[27422:60b] -[UITableViewCell Title]: unrecognized selector sent to instance 0x8f7e540
    2014-06-26 14:45:19.310 TableViewWithPlist[27422:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell Title]: unrecognized selector sent to instance 0x8f7e540'
    *** First throw call stack:
    (
    0 CoreFoundation 0x017f01e4 __exceptionPreprocess + 180
    1 libobjc.A.dylib 0x0156f8e5 objc_exception_throw + 44
    2 CoreFoundation 0x0188d243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3 CoreFoundation 0x017e050b ___forwarding___ + 1019
    4 CoreFoundation 0x017e00ee _CF_forwarding_prep_0 + 14
    5 TableViewWithPlist 0x00002d4e -[CellViewController tableView:cellForRowAtIndexPath:] + 190
    6 UIKit 0x0032811f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
    7 UIKit 0x003281f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
    8 UIKit 0x00309ece -[UITableView _updateVisibleCellsNow:] + 2428
    9 UIKit 0x0031e6a5 -[UITableView layoutSubviews] + 213
    10 UIKit 0x0029e964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    11 libobjc.A.dylib 0x0158182b -[NSObject performSelector:withObject:] + 70
    12 QuartzCore 0x03c5b45a -[CALayer layoutSublayers] + 148
    13 QuartzCore 0x03c4f244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    14 QuartzCore 0x03c5b3a5 -[CALayer layoutIfNeeded] + 160
    15 UIKit 0x00360ae3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
    16 UIKit 0x00276aa7 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
    17 UIKit 0x00275646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    18 UIKit 0x00275518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    19 UIKit 0x002755a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    20 UIKit 0x0027463a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    21 UIKit 0x0027459c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    22 UIKit 0x002752f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    23 UIKit 0x002788e6 -[UIWindow setDelegate:] + 449
    24 UIKit 0x00352b77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    25 UIKit 0x0026e474 -[UIWindow addRootViewControllerViewIfPossible] + 591
    26 UIKit 0x0026e5ef -[UIWindow _setHidden:forced:] + 312
    27 UIKit 0x0026e86b -[UIWindow _orderFrontWithoutMakingKey] + 49
    28 UIKit 0x002793c8 -[UIWindow makeKeyAndVisible] + 65
    29 UIKit 0x00229bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
    30 UIKit 0x0022e667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    31 UIKit 0x00242f92 -[UIApplication handleEvent:withNewEvent:] + 3517
    32 UIKit 0x00243555 -[UIApplication sendEvent:] + 85
    33 UIKit 0x00230250 _UIApplicationHandleEvent + 683
    34 GraphicsServices 0x037e5f02 _PurpleEventCallback + 776
    35 GraphicsServices 0x037e5a0d PurpleEventCallback + 46
    36 CoreFoundation 0x0176bca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    37 CoreFoundation 0x0176b9db __CFRunLoopDoSource1 + 523
    38 CoreFoundation 0x0179668c __CFRunLoopRun + 2156
    39 CoreFoundation 0x017959d3 CFRunLoopRunSpecific + 467
    40 CoreFoundation 0x017957eb CFRunLoopRunInMode + 123
    41 UIKit 0x0022dd9c -[UIApplication _run] + 840
    42 UIKit 0x0022ff9b UIApplicationMain + 1225
    43 TableViewWithPlist 0x000026cd main + 141
    44 libdyld.dylib 0x01e37701 start + 1
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb)


    also muss ich statt searchController = [[UISearchDisplayController alloc]init];
    sondern
    self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:???? contentsController:?????];
  • Die Fehlermeldung ist doch eindeutig:
    2014-06-26 14:45:19.305 TableViewWithPlist[27422:60b] -[UITableViewCell Title]: unrecognized selector sent to instance 0x8f7e540

    UITableViewCell hat keine Property Title. Es wundert mich, dass der Compiler für die folgenden Zeilen keine Warning anzeigt:

    Quellcode

    1. Cell.Title.text = [[QuestionArray objectAtIndex:indexPath.row] objectForKey:@"Name"];
    2. Cell.SubTitle.text = [[QuestionArray objectAtIndex:indexPath.row] objectForKey:@"Number"];

    Die beiden Properties, welche Du sicherlich meinst, heissen textLabel und detailTextLabel.

    Variablen sollte man auch klein schreiben, also cell und nicht Cell.

    Hast Du ein gutes Buch für Objective-C und iOS?

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von MCDan ()