UITableView reloadData don`t work - ios

I get an array of another class, and make changes to this class, but when I do a reloadData not much happens.
What is wrong?
This is my code
#pragma mark TesteFaceCarViewController methods
-(void)valores:(NSMutableArray *)array
{
arrayInfracao = array;
NSLog(#"Quantidade :%d", arrayInfracao.count);
[table reloadData];
}
#pragma mark TableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Passei aqui!");
NSLog(#"Quantidade :%d", arrayInfracao.count);
return arrayInfracao.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Infracao *infracao = [[Infracao alloc] init];
infracao = [arrayInfracao objectAtIndex:indexPath.row];
cell.textLabel.text = infracao.dsInfracao;
return cell;
}

You are welcome!
[self.table reloadData];

Related

How to handle the dynamically inserted UITableView cells with same identifier?

I'm inserting a new row when "+" Button on top of the table view is clicked. It is adding fine. but the cells are reordering when tableview is scrolled up and down. Is there any way to handle this? Here is the complete code.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"TABLE";
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
array1 = [[NSMutableArray alloc]initWithObjects:#"Row1",#"Row2",#"Row3",#"Row4", nil];
table1 = [[UITableView alloc]initWithFrame:CGRectMake(20, 20, 360, 360)style:UITableViewStyleGrouped];
table1.delegate = self;
table1.dataSource = self;
table1.backgroundColor = [UIColor brownColor];
[self.view addSubview:table1];
UIBarButtonItem *plusButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(plusButtonHit)];
self.navigationItem.rightBarButtonItem = plusButton;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array1 count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SecondViewController *secondViewController = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondViewController animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSUInteger count = [array1 count];
if (row < count)
{
return UITableViewCellEditingStyleDelete;
} else
{
return UITableViewCellEditingStyleNone;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[array1 removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (void)plusButtonHit
{
NSIndexPath *indexPath = [[NSIndexPath alloc]init];
indexPath = [NSIndexPath indexPathForRow:array1.count inSection:0];
NSString *newString = [NSString stringWithFormat:#"Row%ld", (long)indexPath.row+1];
[array1 addObject:newString];
[table1 insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
In cellForRowAt method set cell's label text after the if condition block.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
//Set cell's label text here
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
return cell;
change the code like this: set reuseIdentifier:nil
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

Get checkmark values of uitableview after clicking a button

I want to get the values of multiple selection of tableview on clicking a button.I checked lot of stackoverflow questions but unable to get the answer. I know this is already achieved by many people so i request someone to guide me about getting the values from tableview. I am able to select mulitple values from tableview.I am able to see checkmark.
Below is my code
.h file
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) IBOutlet UITableView *specTableView;
#property (nonatomic, retain) NSArray *arForTable;
#property (nonatomic, retain) NSMutableArray *arForIPs;//to capture ids
- (IBAction)btnActionToGetValues:(id)sender;
.m file
- (void)viewDidLoad {
self.arForTable=[NSArray arrayWithObjects:#"value1",#"value2",#"value3",#"value4", nil];
self.arForIPs=[NSMutableArray array];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arForTable count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([self.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arForIPs containsObject:indexPath]){
[self.arForIPs removeObject:indexPath];
} else {
[self.arForIPs addObject:indexPath];
}
[tableView reloadData];
}
//button action for getting values
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(#"ids %#",arForIPs);
}
I am getting this output
ids (
" {length = 2, path = 0 - 1}",
" {length = 2, path = 0 - 2}" )
My expected output is
ids (
"Value1",
"Value2" )
If any one has already worked on this please let me know.
try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arForIPs containsObject:[self.arForTable objectAtIndex:indexPath.row]]){
[self.arForIPs removeObject:[self.arForTable objectAtIndex:indexPath.row]];
} else {
[self.arForIPs addObject:[self.arForTable objectAtIndex:indexPath.row]];
}
[tableView reloadData];
}
on your CellforRowatIndexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([self.arForIPs containsObject:[self.arForTable objectAtIndex:indexPath.row]]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
output you can get
//button action for getting values
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(#"ids %#",arForIPs);
}
I guess you can create one new array,example:
NSMutableArray *arr = [NSMutableArray array];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([self.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
[arr addObject:self.arForTable[indexPath.row]];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
[arr removeObject:self.arForTable[indexPath.row]];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(#"ids %#",arr);
}
What you are seeing in your log is the indexPath value of your cells. The length value is how many items are in the path (2) and the path value is showing the row and section of the cell the user tapped.
If you will use the index path value to get a reference to the cell; you can ask the cell if it has a checkmark turned on. You are also holding the values in your arForTable array. So, you use the index path from the tableView to ask the arForTable array for the value. I might write the didSelectRowAtIndexPath like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *valueFromArray = [[self arForTable] objectAtIndex:[indexPath row]];
if([self.arForIPs containsObject:valueFromArray]){
[self.arForIPs removeObject:valueFromArray];
} else {
[self.arForIPs addObject:valueFromArray];
}
[tableView reloadData];
}
Of course, this will cause your cellForRowAtIndexPath to break. So you will need to update that as well. Perhaps replace this:
if([self.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
with something like:
NSString *valueString = [[self arForTable] objectAtIndex:[indexPath row]];
if([self.arForIPs containsObject:valueString]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=valueString;
Often in a tableView you will find that the array (arForTable) holds the data and since the index property of the array matches the row property of the tableView we can use that match to query the array for the data that corresponds to the tableView row that we are displaying.

Add row in UITableView in prototype cell mode (that's not what my expect)

I have UITableView with prototype cell.I have a label in cells with various values(text value).
when add a new row, some of labels in new rows created with previous cell values not default values:
What can i do?
Is my question clear?
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
gamers = [[NSMutableArray alloc] initWithObjects:#"Player1",#"Player2", nil];
}
- (IBAction)btnAddRow:(id)sender {
[gamers addObject:#"new player"];
[_tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = #"gamerCell";
nTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[nTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
}
cell.lblTitle.text = [gamers objectAtIndex:indexPath.row];
return cell;
}
Please find the updated project in the link
UITableView-dynamic-row-buttons
Hope this helps.
Where do you update the score? if you are not doing, make, according to your logic, and then set this value in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = #"gamerCell";
nTableViewCell *cell = (nTableViewCell *)[tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[nTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
}
cell.lblTitle.text = [gamers objectAtIndex:indexPath.row];
cell.lblScore.text = [NSString stringWithFormat:#"%i", (int)scoreProperty];
return cell;
}

Table View not populating objects

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
I have a tableview with an Array of objects but when I run it. Nothing shows up. Im fairly new to xcode but Im not sure what my mistake is in the code. Can some one help me out?
If the cellForRowAtIndexPath callback is never called, it could be:
you didn't have set the dataSource of your tableview;
you didn't implement the numberOfSectionsInTableView: and/or tableView:numberOfRowsInSection: callbacks.
And if you dequeue your cell, you need to set the text outside the if branch like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
return cell;
}
Change the code like this your code will run,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.GenusNameLabel.text = [[genusName objectAtIndex:indexPath.row] geniusname];
return cell;
}
geniusname is the NSString you stored the name of the person.

UITableView loads but nothing displayed

When I build and run there is nothing displayed. No text just empty cells.
Here is my code:
#import "plungerselection.h"
#implementation plungerselection
#synthesize tableData;
-(void) viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"Turbo", #"Hollow Turbo", #"Single Pad", #"Dual Pad", #"Bullet", nil];
[super viewDidLoad];
}
#pragma mark - TableView Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
return cell;
}
#end
Make sure you have wired up the datasource and delegate connections in Interface Builder.
You are only setting the text in the cell if dequeueReusableCellWithIdentifier: returns nil which in this case doesn't happen (and even if it did it will only happen a few times before all the cells that are needed get in the queue). You should set the text outside of the if (cell == nil)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}

Resources