How do I show multiple custom cells using objective C? - ios

I have a few custom cells that I would like to display information on. I made 2 new cells however it is only returning one cell. The top most cell's identifier is "Cell" and the bottom one is "quantityCell". I would like the product description to be on the top cell and the quantity on the bottom cell. The output I am getting now is only the quantity cell showing, but not the product cell. If I remove the quantity cell, the product cell will show. How should I go about doing this?
Thank you
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *quantityCellIdentifier = #"quantityCell";
UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
self.productName = [inventoryItem getProductTitle];
productTitle = self.productName;
cell.detailTextLabel.text = productTitle;
NSLog(#"productTitle %#", self.productName);
cell.textLabel.text = #"Product Title";
self.productQuantity = [inventoryItem getQuantity];
quantityCell.textLabel.text = #"Quantity";
quantityCell.detailTextLabel.text = self.productQuantity;
/*
// Configure the cell...
// cell.textLabel.text = productTitle;
cell.textLabel.text = #"Product Name";
cell.detailTextLabel.text = productTitle;
*/
return cell;return quantityCell;
}

Problem
The you can't return 2 parameters in the same time on a method. So where you say:
return cell;return quantityCell;
Only cell is being returned, the method ends on the first return.
Solution
You need to make logic which one time returns topCell and the other time bottomCell
So in your case you just need to have indexPath.row modulus 2, which can take any value and returns only 0 on even numbers and 1 on odd numbers.
So your code should be like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"";
if (indexPath.row%2==0) {
//Get the top cell
CellIdentifier = #"Cell";
} else {
//Get bottom cell
CellIdentifier = #"quantityCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
if (indexPath.row%2==0) {
//Customize topCell
self.productName = [inventoryItem getProductTitle];
productTitle = self.productName;
cell.detailTextLabel.text = productTitle;
NSLog(#"productTitle %#", self.productName);
cell.textLabel.text = #"Product Title";
} else {
//Customize bottom cell
self.productQuantity = [inventoryItem getQuantity];
cell.textLabel.text = #"Quantity";
cell.detailTextLabel.text = self.productQuantity;
}
return cell;
}

return cell;
return quantityCell;
The above 2 line you wrote in your function! After return statement the method stop executing further lines.
You could do something like this:
self.productName = [inventoryItem getProductTitle];
self.productQuantity = [inventoryItem getQuantity];
productTitle = self.productName;
if(indexPath.row == 0) {
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.detailTextLabel.text = productTitle;
cell.textLabel.text = #"Product Title";
return cell;
}
else {
NSString *quantityCellIdentifier = #"quantityCell";
UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
quantityCell.textLabel.text = #"Quantity";
quantityCell.detailTextLabel.text = self.productQuantity;
return quantityCell;
}
Hope this helps.. :)

Thank you all, I ended up using a dictionary that returns all of the keys and values to the cells.
NSDictionary *inventoryDictionary = #{
#"Quantity" : productQuantity,
#"Price" : productPriceValue,
#"Product Title" : productTitle
};
NSLog(#"ProductStrings: %#", inventoryDictionary);
NSString *keys = [inventoryDictionary allKeys][indexPath.row];
NSString *providerNameString = keys;
cell.textLabel.text = providerNameString;
NSString *Values = [inventoryDictionary allValues][indexPath.row];
NSString *providerNameValue = Values;
cell.detailTextLabel.text = providerNameValue;
return cell;

Related

cellForRowAtIndexPath not returning cell objective c

Iam facing a issue with the cellForRowAtIndexPath not returning cell.
Below is my service output
(
{
AcNo = 667;
Avg = 2;
Address = New York;
busReviews = (
{
Id = 270;
businessName = "Pharmacy Inc";
comment = "Good Product.";
},
{
Id = 269;
businessName = "Agro Buss Inc";
comment = "Looks like a great product.";
}
);
}
)
When i tried to retrieve the data in a table view for busReviews. It only displays the first record not the second record.
Below is what i have tried. Please help me to find the issue. TIA
- (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"];
}
NSMutableDictionary * thisRow = [detailsarr objectAtIndex:indexPath.row];
times = [thisRow objectForKey:#"busReviews"];
NSMutableDictionary* refilldict = [times objectAtIndex:0];
NSString *column1 = [refilldict objectForKey:#"businessName"];
NSString *column2 = [refilldict objectForKey:#"comment"];
reviewname.text=[NSString stringWithFormat:#"%#%#",#"-by ",column1];
review.text= [NSString stringWithFormat:#"%#",column2];
cell.selectionStyle=UITableViewCellSelectionStyleGray;
return cell;
}
return nil;
}

Inserting a new cell in between the cells in tableview

i here attached a snapshot of my sample output.now it has two cells that is according to arraycount which i used at number of rows in section method.i have calutalted the break time ,now i want to insert one more cells in between that two cells to dispay the break time . here is my code whic i did ....please help me to do this .thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"%#",self.arrData1);
NSDictionary *tempDictionary= [self.arrData1 objectAtIndex:indexPath.row];
//NSLog(#"%#",tempDictionary);
NSString *strFunctionCodeValue =[tempDictionary objectForKey:#"function_code"];
NSString *strStartTimeValue = [tempDictionary objectForKey:#"start_time"];
NSString *strEndTimeValue = [tempDictionary objectForKey:#"end_time"];
NSString *strTotalUnitsValue =[tempDictionary objectForKey:#"total_units"];
NSString *strTotalTimeValue = [tempDictionary objectForKey:#"total_time"];
//NSString *strBreakTimeValue = [tempDictionary objectForKey:#"break_time"];
cell.lblFunctionCodeValue.text = strFunctionCodeValue;
cell.lblStartTimeValue.text = strStartTimeValue;
cell.lblEndTimeValue.text = strEndTimeValue;
cell.lblTotalUnitsValue.text =strTotalUnitsValue;
cell.lblTotalTimeValue.text = strTotalTimeValue;
//cell.lblBreakTimeValue.text = strBreakTimeValue;
return cell;
}

Modifying one cell in CellForRowAtIndexPath changes multiple cells

I have an array of names that I'm showing via tableview. You can select up to a total of 3 names, and you cannot re-select the same names. To do this I implemented the following code in cellForRowAtIndexPath:. When I run the code the names come up fine, but there are multiple red cells with names that I did not select.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3]) {
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Reading a similar problem here, they were saying that it's because the cells are being reused - but if this were true, how is the tableview still displaying the correct names in the correct position?
I tried to simplify the code into this, and still to no avail, there were multiple red cells.
myIP = [NSIndexPath indexPathForRow:0 inSection:0];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if (indexPath == myIP) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
I can post a screenshot if needed. Note: The intended names were correctly labeled with red.
The issue is happening due to cell re-using. When a cell with red background is re-used it'll still be in red background, you are not re-setting it anywhere in your code. You need to put a else case to your if condition used in cellForRowAtIndexPath: method.
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3])
{
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[cell setUserInteractionEnabled:YES];
cell.backgroundColor = [UIColor clearColor];
// Other stuffs
}

how can I edit a cell in a tableView in iOS?

I have implemented a viewController that contains a tableView which display user's data(firstName , lastName , birthday..) fetched from login Facebook or twitter or google Plus .I have made three custom cells : firstNameTableViewCell , lastNameTableViewCell,birthdayTableViewCell : All of this three cells contains a label ans a textfield.I want that when the user touch the corresponding cell , he will be able to change his data.
Technically , I have used the tableView's delegate methods : cellForRowAtIndexPath & didSelectRowAtIndexPath but I don't have what I want : When I open the interface : the label is disappering and when i touch the corresponding cell , nothing that changes .
This my code :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row)
{
case 0 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"genderCell"];
genderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.maleImageView = [cell showGender:self.gender];
cell.femaleImageView =[cell showGender:self.gender];
return cell ;
break;
}
case 1 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"firstNameCell"];
firstNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.firstNameLabel.text = [NSString stringWithFormat:#"%#",self.firstName];
cell.firstNameTextField.hidden =YES ;
return cell;
break;
}
case 2 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"lastNameCell"];
lastNameTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.lastNameLabel.text = [NSString stringWithFormat:#"%#",self.lastName];
cell.lastNameTextField.hidden = YES ;
return cell;
break;
}
case 3 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"BirthDayCell"];
birthDayTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.birthDaylabel.text = [NSString stringWithFormat:#"%#",self.birthDay];
cell.birthDayTextField.hidden = YES ;
return cell;
break;
}
case 4 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"heightCell"];
heightTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.heightUnityLabel.text = [NSString stringWithFormat:#"%#",self.heightUnity];
self.height = [cell.heightTextField.text floatValue];
return cell;
break;
}
case 5 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"weightCell"];
weightTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.weightUnityLabel.text = [NSString stringWithFormat:#"%#",self.weightUnity];
self.weight = [cell.weightTextField.text floatValue];
return cell;
break;
}
default:
break ;
}
return nil ;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%ld",indexPath.row);
if (indexPath.row == 1) {
firstNameTableViewCell *cell = [firstNameTableViewCell new];
cell.firstNameLabel.hidden = YES ;
cell.firstNameTextField.hidden = NO ;
cell.firstNameTextField.text = cell.firstNameLabel.text ;
[self.userDataTableView reloadData];
}
else if (indexPath.row == 2) {
lastNameTableViewCell *cell = [lastNameTableViewCell new];
cell.lastNameLabel.hidden = YES ;
cell.lastNameTextField.hidden = NO ;
cell.lastNameTextField.text = cell.lastNameLabel.text ;
[self.userDataTableView reloadData];
}
else if (indexPath.row == 3) {
birthDayTableViewCell *cell = [birthDayTableViewCell new];
cell.birthDaylabel.hidden = YES ;
cell.birthDayTextField.hidden = NO ;
cell.birthDayTextField.text = cell.birthDaylabel.text ;
[self.userDataTableView reloadData];
}
}
Inside:
-(void)tableView:tableView didSelectRowAtIndexPath:indexPath
It is much better if you'll just use the indexPath to return the cell like:
firstNameTableViewCell *cell = (firstNameTableViewCell *)[tableView cellForRowAtIndexPath:(NSIndexPath)];
..
cell.firstNameTextField.hidden = NO ;
cell.firstNameTextField.text = cell.firstNameLabel.text;
// but do not reload the table yet..
Instead make cell.firstNameTextField as the first responder and let the user enter necessary data..
[cell.firstNameTextField becomeFirstResponder];
if the user is done editing and you have database for that, update your database on DONE, then your table dataSource (self.tableUserData), only then you need the [self.userDataTableView reloadData];, and i think you dont really want to reload the whole table for that single edit,[self.userDataTableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:(UITableViewRowAnimation)] reloading only the target indexPath is much more suitable for that..
-reloadData means 'reloading everything in the table based from your array data source'. meaning settings of values made inside:
-(void)tableView:tableView willDisplayingCell:cell forRowAtIndexPath:indexPath
and
-(UITableViewCell *)tableView:tableView cellForRowAtIndexPath:indexPath
ex:
cell.firstNameLabel.text = [NSString stringWithFormat:#"%#",self.firstName];
cell.lastNameTextField.hidden = YES ;
will be refreshed and set as is...
Edit
Also i've noticed that you didn't allocate your cells if nil after dequeueReusableCellWithIdentifier(i wonder why your app is not crashing because of it), anyway it is good for the memory to reuseCells like:
firstNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
cell = [[firstNameTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

Displaying NSArray in UITableView (iOS)

I want to display in UITableView array like:
(
1,
{
date = 1351876762;
ncom = 0;
nid = 11739814;
"read_ncom" = 0;
text = "<div class=\"wikiText\"><!--4-->New note text </div>";
title = lol;
uid = 3795852;
}
)
Cells should have a label with "title" from this array.
How can I make that?
P.S.:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *textCell = [[array objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.textLabel.text = textCell;
return cell;
}
returns signal SIGABRT.
Thanks.
[EDIT]
Your array seems to be weird. Try following and let me know the result.
NSString *textCell = [[array objectAtIndex:1] objectForKey:#"title"];
You should have cell with identifier "Cell" in your table (if you have storyboard) or create this cell manually
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[array objectAtIndex:1] objectforkey:#"title"];
try the above code
Replace this :
NSString *textCell = [[[array objectAtIndex:indexPath.row] objectForKey:#"1"]objectForKey:#"title"];

Resources