当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - ReloadTableView with new NSMutableArray

问题描述:

I am using customTableView.I want to reload my tableViewCell with new data from my NSMutableArray.How can i do that?Here is my current Code for the tableView :

tableView.m

//

// GameTableView.m

//

// Created by Mahmudul hasan on 4/6/11.

// Copyright 2011 JU. All rights reserved.

//

#import "GameTableView.h"

#import "BeginingCell.h"

@implementation GameTableView

@synthesize XmlManipulatorObject,QuestionMutableArray,dictionary;

@synthesize _tableView=tableView;

#pragma mark -

#pragma mark Table view data source

-(void)viewDidLoad

{

[super viewDidLoad];

XmlManipulatorObject=[[xmlManipulator alloc] init];

QuestionMutableArray=[[XmlManipulatorObject ReadXml] retain];

dictionary=[[NSMutableDictionary alloc] init];

dictionary = [QuestionMutableArray objectAtIndex:0];

}

-(IBAction) goBack:(id) sender{

[self.parentViewController dismissModalViewControllerAnimated:YES];

}

-(void) btnAction:(id) sender {

NSString *str = ((UIButton*)sender).titleLabel.text;

UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:str message:str

delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil ];

[dictionary removeAllObjects];

[alrt show];

[alrt release];

if(dictionary==nil)

{

NSLog(@"dictionary is nill now");

}

else {

NSLog(@"dictionary is not nil");

}

}

//Method to iterate trough a NSMutableDictionaries in a NSMutableArray

-(NSMutableArray*)GetQuestionByKey:(NSInteger *)key :(NSMutableArray *)Array

{

NSMutableArray *arr;

for (dictionary in Array) {

if (dictionary!=nil) {

arr=[[NSMutableArray alloc] init];

[arr addObject:[dictionary objectForKey:@"option1"]];

}

}

return arr;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;

}

// Customize the number of rows in the table view.

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

return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"%@",[dictionary objectForKey:@"question"]);

NSLog(@"%@",[dictionary objectForKey:@"option1"]);

NSLog(@"%@",[dictionary objectForKey:@"option2"]);

NSLog(@"%@",[dictionary objectForKey:@"option3"]);

NSLog(@"%@",[dictionary objectForKey:@"option4"]);

static NSString *CellIdentifier = @"BeginingCell";

BeginingCell *cell=(BeginingCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"BeginingCell" owner:self options:nil ];

for(id CurrentObject in topLevelObjects)

{

if ([CurrentObject isKindOfClass:[BeginingCell class]]) {

cell=(BeginingCell *) CurrentObject;

cell.selectionStyle = UITableViewCellSelectionStyleNone;

break;

}

}

}

// Configure the cell.

if(indexPath.row==0)

{

cell.myImageView.image = [UIImage imageNamed:@"man_kirstie_alley.jpg"];

cell.SectionTitle.text=[dictionary objectForKey:@"question"];

cell.Option1.text=[dictionary objectForKey:@"option1"];

cell.Option2.text=[dictionary objectForKey:@"option2"];

cell.Option3.text=[dictionary objectForKey:@"option3"];

cell.Option4.text=[dictionary objectForKey:@"option4"];

///For showing the option idntity

[cell.button1 setTitle:@"A." forState:UIControlStateNormal];

[cell.button2 setTitle:@"B." forState:UIControlStateNormal];

[cell.button3 setTitle:@"C." forState:UIControlStateNormal];

[cell.button4 setTitle:@"D." forState:UIControlStateNormal];

///For selecting the correct answer

[cell.ansBtn1 setTitle:@"A" forState:UIControlStateNormal];

[cell.ansBtn2 setTitle:@"B" forState:UIControlStateNormal];

[cell.ansBtn3 setTitle:@"C" forState:UIControlStateNormal];

[cell.ansBtn4 setTitle:@"D" forState:UIControlStateNormal];

[cell.ansBtn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

[cell.ansBtn4 addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];

}

return cell;

}

-(void)viewDidUnload

{

[self dealloc];

}

@end

网友答案:

call

[tblView reloadData];

when you are updating your mutable array.

分享给朋友:
您可能感兴趣的文章:
随机阅读: