Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh table view?

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
like image 881
Feroc Avatar asked Jul 22 '13 12:07

Feroc


People also ask

How do you refresh a table in JavaFX?

Since JavaFX 8u60 you can use(assuming tableView is an instance of TableView class): tableView. refresh();

How do I refresh UICollectionView?

Since iOS 10, the UITableView and UICollectionView classes have a refreshControl property. You can add a refresh control to a table or collection view by assigning an instance of the UIRefreshControl class to this property.

What does tableView reload data do?

reloadData()Reloads the rows and sections of the table view.


1 Answers

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

like image 154
Chris Tetreault Avatar answered Nov 06 '22 05:11

Chris Tetreault