18

I think I have a pretty easy task, but somehow it doesn't want to work. I am a total beginner in objective-c, so I guess it's just a small mistake. I still don't really know what I do, currently it's more like copy&paste programming. Like I don't really know if I need the IBOutlet in the interface or as a property or as both.

What I have:

A ViewController with a Button, a Label and a Table View. The button connects to a sharepoints server and reads a list and adds the value to an array. This part works.

Delegate and DataSource outlet is connected to the View Controller.

What I want:

The array should be the datasource of the Table View, so I just want it to refresh after I've read the new data in the array. The test data I add in the viewDidLoad function to the array, shows up. So I guess I somehow connected the array to the table view.

I'll give you the full code:

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UILabel *output;
    IBOutlet UITableView *tableView;
    NSMutableData *webData;
    NSString *finaldata;
    NSString *convertToStringData;
    NSMutableString *nodeContent;
}
@property (nonatomic, retain) UILabel *output;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
-(IBAction)invokeService:(UIButton *) sender;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *foundUrlaub;
}

@end

@implementation ViewController

@synthesize output;


- (void)viewDidLoad
{
    [super viewDidLoad];

    // SOME TEST DATA... THIS SHOWS UP IN MY TABLE VIEW
    foundUrlaub = [[NSMutableArray alloc]init];
    [foundUrlaub addObject:@"first cell"];
    [foundUrlaub addObject:@"second cell"];
    [foundUrlaub addObject:@"third cell"];
}

-(IBAction)invokeService:(UIButton *) sender
{
    // connection to sharepoint
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse");
    [webData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"didReceiveData");
    [webData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with the Connection");
    NSLog(error.description);
}

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    NSLog(@"canAuthenticateAgainstProtectionSpace");
    return YES;
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"didReceiveAuthenticationChallenge");
    NSURLCredential *credential = [NSURLCredential credentialWithUser:@"XXXXXX" password:@"XXXXXX" persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    convertToStringData = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=ows_Title=')(.*)(?=' ows_MetaInfo)" options:0 error:NULL];

    NSArray *matches = [regex matchesInString:convertToStringData options:0 range:NSMakeRange(0, [convertToStringData length])];

    // HERE I LOAD SOME DATA IN THE ARRAY
    [foundUrlaub removeAllObjects];
    for (NSTextCheckingResult *match in matches)
    {
        NSRange matchRange = [match rangeAtIndex:1];
        NSString *matchString = [convertToStringData substringWithRange:matchRange];
        NSLog(@"Match: %@", matchString);
        [foundUrlaub addObject:matchString]; // <- ADDS 3 STRINGS TO ARRAY
    }

    // THIS DOES NOT WORK!
    [tableView reloadData];

}


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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [foundUrlaub count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [foundUrlaub objectAtIndex:indexPath.row];
    return cell;
}

@end
7
  • Not an Xcode question.
    – user529758
    Commented Jul 22, 2013 at 12:26
  • you are getting data from server using NSURLConnection asynchronously so it uses separate threads for sending and receiving data. Commented Jul 22, 2013 at 12:35
  • try doing as i have told and see if its working or not? Commented Jul 22, 2013 at 12:36
  • @feroc r u getting data in foundUrlaub have u printed it. Commented Jul 22, 2013 at 12:55
  • tble view showing three rows right now that is static values? Commented Jul 22, 2013 at 12:57

4 Answers 4

36

try using [_tableView reloadData]; You didn't @synthesize your tableView in your .m so you have to use the autosynthesized identifier

2
  • 6
    @Feroc I prefer using [self.tableView reloadData] instead of the underscore. It helps me remember the tableView is a property of the class. As a matter of fact I recommend never synthesizing anymore (as compiler will do this for you) and always using the self. instead of the underscore
    – MobileMon
    Commented Aug 14, 2013 at 21:00
  • It successfully reloads but does not display the new data until I tap the cells, any idea why? Commented Jun 19, 2014 at 12:17
3

Your code looks fine ,

make sure

  1. IBOutlet for tableView is connected properly
  2. Datasource and delegates from nib is connected to the files owner

A must watch and a must read for you

7
  • I don't have any nib, I am using the storyboard, which confuses me even more. I also don't really know how to connect the IBOutlet to the tableView. Isn't it enough to set the "Restoration ID" to "tableView"? Or do I have to drag&drop stuff to somewhere?
    – Core_F
    Commented Jul 22, 2013 at 12:39
  • storyboard and nib both do the same in case of outlet.Just right click on the tableview and drag the referencing outlet to the files owner /viewcontroller ,There it will shows tableview and select it
    – Lithu T.V
    Commented Jul 22, 2013 at 12:41
  • an outlet lets the code knows that reference point to that instance in the storyboard/nib
    – Lithu T.V
    Commented Jul 22, 2013 at 12:42
  • Good info. I've dragged it to the View Controller and was able to select "tableView". So it now says: "Referencing Outlets - tableView - View Controller"... but it still doesn't refresh the table view. :(
    – Core_F
    Commented Jul 22, 2013 at 12:46
  • that is because datasource is not connected.Do the same for datasource in the pop up appearing for the tableview
    – Lithu T.V
    Commented Jul 22, 2013 at 12:47
1

You have to connect the Outlet in the Interfacebuilder to your UITableview tableView.

1
  • Open up your .xib file, and select "Files owner" on the left pane. On the right pane click on the last right icon (the right arrow) and select your "tableview" element from the bubble right and drag the line onto your tableview. Commented Jul 22, 2013 at 12:40
-5

You are trying to reload table from separate thread. so UIView can be changed only from main thread so do something like this:

[tableView performSelectorOnMainThread:@selector(reloadData)
                            withObject:nil
                         waitUntilDone:YES];
4
  • 3
    This is not where the problem lies.
    – Lithu T.V
    Commented Jul 22, 2013 at 12:38
  • Totally agree with Lithu T.V what are you talking about @ArmaanStranger? There is no separate thread. -1
    – Popeye
    Commented Jul 22, 2013 at 12:40
  • i can't see any other problem. if you have no problem in loading tableview at start of your view. Commented Jul 22, 2013 at 12:42
  • Sorry, doesn't change anything.
    – Core_F
    Commented Jul 22, 2013 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.