Cannot seem to pass an array to uitableview xcode - ios

I am trying to display the contents of an array onto a uitableview. It works fine if I assign the values individually but when I try to display them from an array xcode exits with nothing in the debugging report except (lldb). It compiles and runs great until I go to the page with the tableview. I've looked up the error and it seems to pertain to memory allocation but I am unable to manually release anything due to having to have ARC enabled for some JSON classes to operate. Can anyone take a look and see what possibly might be the issue.
- (void)viewDidLoad
{
[super viewDidLoad];
theArray = [[NSArray alloc] initWithObjects:#"1","2","3","4",nil];
// Do any additional setup after loading the view.
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [theArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Set up the cell...
self.recentSearchesTable.backgroundColor =[UIColor clearColor];
self.recentSearchesTable.separatorColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15];
cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// open a alert with an OK and cancel button
NSString *alertString = [NSString stringWithFormat:#"Clicked on row #%d", [indexPath row]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:#"" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
[alert show];
}

In viewDidLoad your array contains non-object items, change it too:
theArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
Note the # before each item in the array, indicating that they're NSString literals (and not c strings).

You did two mistake here first you need to initialize array in this way.
theArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
and another mistake is you need to initialize cell when it is nil
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if you want whole code then like this way you need to implement.
- (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] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Set up the cell...
cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}

First of all you do not need to disable ARC for your project due to an external class.You can exclude these classes by setting -fno-objc-arc flag.
Back to your answer
You should alloc a new cell if it can be reeused from a previous one.You should alloc you new cell if dequeueReusableCellWithIdentifier:CellIdentifier does not return a cell.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier] autorelease];
}
Apart from that when you init your datasource array in viewDidLoad you need to add # before each element to indicate NSString.

Related

In UItableview, when I scroll table values are miss placing(values selecting from picker)

Here I am trying to do dynamic UITableViewCell with UIPickerView.
Step 1:
In Custom cell, I took 1 label and 1 UITextField.
Step 2:
used downpickerview library for data displaying and data fetching.
Step 3:
using below code I can able to select data, but after that, if I scroll UITableView data will be miss placing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"Cell";
customCell *cell1=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell1 == nil)
{
cell1 = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row];
self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal];
[self.downPicker addTarget:self action:#selector(measurementSelected:) forControlEvents:UIControlEventValueChanged];
[cell1.contentView addSubview:self.downPicker];
return cell1;
}
Please help me on this.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"DETAILS";
_dict = [[NSMutableDictionary alloc]init];
arrProductTitle = [[NSMutableArray alloc]initWithObjects:#"title0",#"title1",#"title2",#"title3",#"title4",#"title5",#"title6",#"title7",#"title8",#"title9",#"title10",#"title11",#"title12",#"title13",#"title14",#"title15", nil];
arrProductVal = [[NSMutableArray alloc]initWithObjects:#"0",#"1",#"2",#"3",#"4",#"5",nil];}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arrProductTitle.count-1;}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"Cell";
customCell *cell1=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell1 == nil)
{
cell1 = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row];
NSLog(#"%#",_dict);
NSLog(#"%#",[NSString stringWithFormat:#"%ld",(long)indexPath.row]);
if (_dict[[NSString stringWithFormat:#"%ld",(long)indexPath.row]]) {
cell1.productvalueTextfield.text =[_dict valueForKey:[NSString stringWithFormat:#"%ld",(long)indexPath.row]];
}
else {
cell1.productvalueTextfield.text = #"";
self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal];
self.downPicker.tag = indexPath.row;
[self.downPicker addTarget:self action:#selector(measurementSelected:) forControlEvents:UIControlEventValueChanged];
[cell1.contentView addSubview:self.downPicker];
}
return cell1;}
-(void)measurementSelected:(id)dp {
NSString* selectedValue = [dp text];
NSString* selectedIndex = [NSString stringWithFormat:#"%ld",(long)[dp tag]];
[_dict setValue:[dp text] forKey:selectedIndex];
NSLog(#"_dict: %#",_dict);
NSLog(#"SELECTED TAG:::::::%ld",[dp tag]);
NSLog(#"SELECTED VALUE:::::::%#",selectedValue);
NSLog(#"SELECTED INDEX VALUEEEEEEEEEEE:::::::%ld",[dp selectedIndex]);}
https://github.com/gvniosdev/Dynamic-UItableview-with-Picker-Selection
UITableViewCell values are misplacing because you haven't set the data for other cells, You need to store the values in an array and just update the values from there and it will work. :)
Something is not right with your code:
cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row];
self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal];
[self.downPicker addTarget:self action:#selector(measurementSelected:) forControlEvents:UIControlEventValueChanged];
[cell1.contentView addSubview:self.downPicker];
Bear in mind that in iOS, a tableView will reuse its cells. So when you scroll your UITableView, it will reuse the old cells which were created before, and
[cell1.contentView addSubview:self.downPicker]; Will be executed everytime a cell is reused, as a result, you will end up having many downPicker objects in one cell.

Text not being displayed in cells

Trying to get my app to display text in its cells. Been scratching my head for a while. This is the code used to display the text but nothing appears. Any advice?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.section == 0) {
NSArray *titles = #[#"Quick Glance", #"Your Home", #"Invites"];
cell.textLabel.text = titles[indexPath.row];
}
cell.backgroundColor = [UIColor clearColor];
return cell;
}
If you have not add the following code in ViewDidLoad() method, please do so.
self.tableView.dataSource = self;
self.tableView.delegate = self;
Firstly you should break this out differently so that you are not re-creating your backing store every time you are producing a cell.
More code would be required in order to ascertain exactly what the issue but at minimum you could put the following in to verify this.
#implementation ViewController {
NSArray *_titles;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_titles = #[#"Quick Glance", #"Your Home", #"Invites"];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [_titles count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = titles[indexPath.row];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
This is the bare minimum you will need above in order to show some information in the tableview
Now the question is are you using a UITableViewController where it already has set the dataSource and delegate methods or are you using a UIViewController where the dataSource and delegate have not been set (either in code or through XIB or Storyboard)?
If you do not have the delegates set up you need to set them up either through the storyboard or through the code, and if through code and using UIViewController make sure you have a reference to the UITableView otherwise you will not be able to set the datasource.

iOS tableView cell caching issues not loading every cell

I'm having a bit of difficulty understanding what is going wrong and how to fix this.
-(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];
NSUInteger row = [indexPath row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.text = [delegate.destinationArray1 objectAtIndex:row];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
[cell.contentView addSubview:where];
return cell;
}
This doesn't work properly but this does:
-(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];
}
NSUInteger row = [indexPath row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.text = [delegate.destinationArray1 objectAtIndex:row];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
[cell.contentView addSubview:where];
return cell;
}
They both get populated by "delegate.destinationArray1" but when all the code is inside the curly braces of
if(cell == nil)
the list gets unordered and repeats cells and misses some out. I can't use the latter way as it creates a MASSIVE memory leak when scrolling.
Any ideas?
I did the exact same thing when I started using UITableViews. The reason for the memory leak is that in the second implementation (the one that works) you are actually creating every cell, every single time. Let me try to explain a bit more.
You never want to set content of a cell between the (cell == nil). The reason for this is the reuseIdentifier. If the table needs to display a new cell it will grab one and see if it has already been alloced/inited. If it has it will just use it. If that is the case the content will already be set in the cell you grabbed and you are not telling it to set it any differently.
between the (cell == nil) only create and establish the view. Not the content. All content should be set after. So then no matter what cell it grabs it can always set the content. So this is what you want:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) // or (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
where.tag = 1;
[cell addSubview:where];
}
NSUInteger row = indexPath.row;
UILabel *where = [cell viewWithTag:1];
where.text = [delegate.destinationArray1 objectAtIndex:row];
return cell;
}
I just coded this in StackoverFlow so sorry if there are any small syntax errors.
The cell object is reused or created by the first statement. After checking cell for nil and creating a cell, you must not create another cell.
So delete the line
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
which comes after
NSUInteger row = [indexPath row];
and you'll work with the correct cell object.
When the tableview reuses cells it is based on the CellIdentifier. The tableview doesn't care what attributes you've set on the cell. In the first case the reuse it happening and it recognizes a cell it can use but that cell has the wrong information on it.
What I do is subclass UITableViewCell and do all the work inside of that class. Here is a quick snippet
#implementation AlertCell
//Custom init method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withHeight:(float)height {
//Whatever you need to do
}
//Place the views
- (void)layoutSubviews {
}
//Custom Setter method
- (void)setAlert:(CWAlert *)incomingAlert withAssets:(NSDictionary *)assets {
}
#end
Then you do something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier;
CWAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[AlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier withHeight:[self convertAssetsLengthToCellHeight:assetsLength]];
UIView *selectedView = [[UIView alloc] init];
selectedView.backgroundColor = [UIColor colorWithHexString:#"F6F6F6"];
cell.selectedBackgroundView = selectedView;
}
NSDictionary *alertInfo = [AlertCell getNeededCellAssets:alert];
[cell setAlert:alert withAssets:alertInfo];
return cell;
}
I can show more code from the subclass if needed.

UITableView appears with empty cells

I have a problem displaying UITableView: Some cells are empty, and content in cells becomes visible only after scrolling (if empty cell scrolls out of the screen and then goes back). I Can't understand what might be the problem.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateFilesList];
[tableView reloadData];
}
- (void) viewDidAppear:(BOOL)animated
{
animated = YES;
[self updateFilesList];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.filesList retain];
NSString *title = [self.filesList objectAtIndex:indexPath.row];
title = [title stringByDeletingPathExtension];
title = [title lastPathComponent];
if (title.length >33) {
title = [title substringFromIndex:33];
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.imageView setImage:[UIImage imageNamed:#"oie_png-1.png"]];
cell.textLabel.text = title;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
Thank you in advance for your suggestions!
Well, you have the cell customization code that happens before you create the cell.
Change it like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.filesList retain];
NSString *title = [self.filesList objectAtIndex:indexPath.row];
title = [title stringByDeletingPathExtension];
title = [title lastPathComponent];
if (title.length >33) {
title = [title substringFromIndex:33];
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// This part creates the cell for the firs time
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// This part customizes the cells
[cell.imageView setImage:[UIImage imageNamed:#"oie_png-1.png"]];
cell.textLabel.text = title;
return cell;
}
The problem is that you make
[cell.imageView setImage:[UIImage imageNamed:#"oie_png-1.png"]];
cell.textLabel.text = title;
before
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
change the order. What is happening is that the first time you execute the table View you never do the title before the alloc. When you reuse cell it works because the cell != nil
You need to put these lines
[cell.imageView setImage:[UIImage imageNamed:#"oie_png-1.png"]];
cell.textLabel.text = title;
after the if(){...} condition.
In the first pass cells are nil. Put those lines before the if does nothing. This is why you see empty cells.
A simple question
Why do you call [self.filesList retain] ?
Hope it helps.

Passing data to table cells

I'm trying to pass values to a table cell but when I replace the string with userdata.title
it shows up blank. userdata is an object of another class UserData, which is external and I imported it. UserData is accessed from all views to pass objects around from one controller to another. Now, how do I pass value to a table, and how do I keep it if I decide to add more values through my textfield?
- (void)viewDidLoad
{
tabledata = [[NSMutableArray alloc] initWithObjects:#"hello", nil];
tablesubtitles = [[NSMutableArray alloc] initWithObjects:#"hello", nil];
[super viewDidLoad];
//if I use "userdata.title" in replacemnent of #"hello" above, the table show up blank
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//--------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"homeworkcell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"homeworkcell"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
//static NSString *CellIdentifier = #"Cell";
/* if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}*/
// Configure the cell.
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:#"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--
return cell;
}
To pass your data from one ViewController (vc1 for example) to another (vc2), you can set a property it the destination controller. So for the UIViewController above, you should add in the .h
#property (nonatomic, retain) UserData *tableDatas;
Then, in the view controller vc1, you will have:
vc2.tableDatas = userdatas;
Finally, in the UITableViewDelegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you can just use:
cell.textLabel.text = [self.tableDatas objectAtIndex:indexPath.row];
There should be some adjustment but the main parts are here.
Good luck.

Resources