I'm trying to dynamically add rows to UITableView.
- added (displayed):adding items during viewDidLoad
- Not added (displayed): Add items after AFNetworking call is complete
This is the state of
when insertRowsAtIndexPaths to tableView instance
I think I'm trying to keep down the points that I tend to fall in love with, such as main threading.
I am not sure why the second item is not added.
I would appreciate it if someone could give me a hint.
=====
ViewController.h
#import @ interface ViewController:UIViewController @end
ViewController.m
# import "ViewController.h" #import @ interface ViewController() @property IBOutlet UITableView*tableView; @property NSMutableArray* cells; @end @implementationViewController{ }; static NSSstring* CellIdentifier=@"tableCell"; - (void) viewDidLoad { superviewDidLoad; NSLog(@"viewDidLoad"; self.cells = [[NSMutableArray alloc] init]; NSSstring*url=@"https://yahoo.co.jp"; [self.cells addObject:url]; NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:path] with RowAnimation:UITableViewRowAnimationTop]; [self.tableView endUpdates]; self refresh; } - (void) refresh { NSSstring*url=@"https://httpbin.org/get"; AFHTTPSessionManager*manager = AFHTTPSessionManager; manager.responseSerializer.acceptableContentTypes=[NSSetSetWithObject:@"application/json"; [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask*_Nonull task, id_Nullable responseObject)} dispatch_async(dispatch_get_main_queue(),^{ NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0]; [self.cells addObject:url]; [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:path] with RowAnimation:UITableViewRowAnimationTop]; [self.tableView endUpdates]; NSLog(@"%@",url); self.tableView reloadData; }); NSLog(@"end:refresh"); } failure:^(NSURLSessionDataTask*_Nullable task, NSError*_Nonnull error) { NSLog(@"failed1-error=%@", error.localizedDescription); }]; } - (NSInteger) numberOfSectionsInTableView: (UITableView*)tableView { return1; } - (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section { return self.cells.count; } - (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath { NSLog (@"cellForRowAtIndexPath"); UITableViewCell* cell= [tableView requestReusableCellWithIdentifier: CellIdentifier]; NSInteger row=indexPath.row; UILabel*label=[cell viewWithTag:4]; label.text=self.cells [row]; NSLog(@"%ld:%@",self.cells.count, label.text); return cell; } @end
2017-02-0306:44:25.242353 test [11678:2443409] viewDidLoad 2017-02-0306:44:25.330775 test [11678:2443409] cellForRowAtIndexPath 2017-02-0306:44:25.334614 test [11678:2443409]1: https://yahoo.co.jp 2017-02-0306:44:26.134155 test [11678:2443409] end:refresh 2017-02-0306:44:26.134275 test [11678:2443409] https://httpbin.org/getios objective-c
Extract from the method refresh
.
NSIndexPath*path=[NSIndexPathindexPathForRow:0 inSection:0];
[self.cells addObject:url];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:path] with RowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
In NSMutableArray
, cells
adds url
to the end of the array.However, the tableView
line is inserted in the first line.Data and view do not match.
However, even with this code, it still behaves strangely (https://httpbin.org/get
is added instead of https://yahoo.co.jp
), but the table has two rows (should be).
If you look at the NSLog
output (I'll decide what it is and proceed), you'll see
You can see the above.2 and 3 are contradictory, but I don't know why they are contradictory.
The code provided is not sufficient and the required lines are omitted.I don't want you to write a code that doesn't directly relate to the content of the question, but if you omit the relevant lines, I just don't know how to deal with them.
#import "ViewController.h"
#import
For example, what is this second line?That's right.
© 2024 OneMinuteCode. All rights reserved.