Reputation: 99
I have an issue with reloading a UITableView
when calling from other viewcontroller. I have called a service from viewcontroller.m
and the results are displayed in tableviewcontroller.m
. Here is my problem, I have a button in UITableViewCell
, when clicked on a button, the table should be reloaded or refreshed.
Below is my code:
viewController.m
NSString * post = [[NSString alloc]initWithFormat:@"country=%@&state=%@&userId=%@",_txtSearchField.text,UserId];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"url"]];
tableviewcontroller.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell)
{
cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *arr=[[profileArr objectAtIndex:indexPath.row] allKeys];
cell.lblAge.text = [[[profileArr objectAtIndex:indexPath.row] valueForKey:key] valueForKey:@"Age"];
cell.lblLocation.text = [[[profileArr objectAtIndex:indexPath.row] valueForKey:key] valueForKey:@"Address"];
cell.lblProfession.text = [[[profileArr objectAtIndex:indexPath.row] valueForKey:key] valueForKey:@"Occupation"];
}
-(void)buttonclick:(id)sender
{
UIButton *btn = sender;
NSArray *arr=[[profileArr objectAtIndex:btn.tag] allKeys];
NSString *key=[arr objectAtIndex:0];
NSMutableDictionary *userDict=[[profileArr objectAtIndex:btn.tag] objectForKey:key];
NSString *str = [userDict objectForKey:@"UserId"];
[self callbuttonService:str];
}
-(void)callbuttonService:(NSString *)OtherUserId
{
//service is called
}
#pragma mark - MRConnection Delegate Methods
- (void)jsonData:(NSDictionary *)jsonDict
{
[SVProgressHUD dismiss];
NSMutableArray *jsonArr1;
jsonArr1=[jsonDict objectForKey:@"DataTable"];
if (![jsonArr1 isEqual:[NSNull null]]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Employee" message:@"Sucess" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
[alertController.view setTintColor:[UIColor blackColor]];
}
else{
[SVProgressHUD showErrorWithStatus:@"Something went wrong!"];
}
}
Upvotes: 1
Views: 127
Reputation: 896
According to your question, you have call service in viewcontroller.m file and you want to reload tableview in tableviewcontroller.m file. Please try this. keep coding.
viewController.m
-(void)callbuttonService:(NSString *)OtherUserId
{
//service is called
}
#pragma mark - MRConnection Delegate Methods
- (void)jsonData:(NSDictionary *)jsonDict
{
[SVProgressHUD dismiss];
NSMutableArray *jsonArr1;
jsonArr1=[jsonDict objectForKey:@"DataTable"];
if (![jsonArr1 isEqual:[NSNull null]]) {
NSDictionary *DictionaryData = [[NSDictionary alloc]init];
[DictionaryData setValue:jsonArr1 forKey:@"data"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"tableReload" object:nil userInfo:DictionaryData];
}
else{
[SVProgressHUD showErrorWithStatus:@"Something went wrong!"];
}
}
tableviewcontroller.m
-(void)viewDidLoad() {
super.viewDidLoad()
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableReloadNotification:) name:@"tableReload" object:nil];
}
- (void) tableReloadNotification:(NSNotification *) notification {
NSDictionary *info = notification.userInfo;
if (info != nil) {
NSMutableArray *jsonArr1 = [info valueForKey:@"data"];
tableview.reloadData();
}
}
Upvotes: 1
Reputation: 3206
Have you tried just calling [tableView reloadData]
?
Also I fail to see how you add a button to your default UITableViewCell
. Maybe you want this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
// check if correct cell was tapped
if (indexPath.section == 0 && indexPath.row == 0)
{
[tableView reloadData];
}
}
When you implement this method, it will be called whenever you tap a UITableViewCell
, you won't need a button or something.
Of course, you're free to bind the [tableView reloadData]
to any other action in your app, e. g. a button in the toolbar or something.
Upvotes: 0
Reputation: 234
You can use NSNotificationCenter for reload table view from another class. When your web service respond successfully then post your local notification.
Upvotes: 0