how to change the UITableViewCell - ios

I have 2 tableViewCell in tabbar. copy values ​​from one button to another, and if the values ​​match the counter is incremented, and the Cell is removed. In managedCoreData
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * result = nil;
static NSString * OrederTableViewCell = #"OrderTableViewCell";
result = [tableView dequeueReusableCellWithIdentifier:OrederTableViewCell];
if (result == nil)
{
result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OrederTableViewCell];
result.selectionStyle = UITableViewCellSelectionStyleNone;
}
Order * order = [self.orderFRC objectAtIndexPath:indexPath];
result.textLabel.text = order.name;
result.imageView.image = [UIImage imageWithData:[order imageSmall]];
self.count = 1;
if ([order.name characterAtIndex:indexPath.row] == [order.name characterAtIndex:indexPath.row+1])
{
self.count++;
[[self managedObjectContext]deleteObject:order];
}
result.detailTextLabel.text = [NSString stringWithFormat:#"X = %d",self.count];
return result;
}

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;
}

Hide Custom Cell in TableView

I have the following implementation, where I am adding a footerview as a customcell. However, if there is no content, I do not want to show the last cell, if there is I want to show it.
In my current implementation, it always displays the last cell.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.adminOrderElements count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return adminOrderElements[section].products.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < adminOrderElements[indexPath.section].products.count)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray * tempArray = adminOrderElements[indexPath.section].products;
cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:#"productname"];
return cell;
}
else
{
static NSString *CellIdentifier = #"FooterCell";
FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
{
cell.footerLabel.text = [NSString stringWithFormat:#"Notes: %#", adminOrderElements[indexPath.section].notes];
}
else
{
cell.heightConstraints.constant = 1;
cell.footerLabel.text = #"";
}
return cell;
}
}
There is one option that you can make cellHeight to 0.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(CHECK_IF_NO_CONTAIN)
return 0;
return 40;// Normal height as you want
}
I think you can add condition in else clause like if certain condition is met then you want to display this cell other wise not...
if (indexPath.row < adminOrderElements[indexPath.section].products.count)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray * tempArray = adminOrderElements[indexPath.section].products;
cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:#"productname"];
return cell;
}
else //Here add your condition here if you have content for the section like I think this condition might work [adminOrderElements[indexPath.section].notes length]>0
{
static NSString *CellIdentifier = #"FooterCell";
FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
{
cell.footerLabel.text = [NSString stringWithFormat:#"Notes: %#", adminOrderElements[indexPath.section].notes];
}
else
{
cell.heightConstraints.constant = 1;
cell.footerLabel.text = #"";
}
return cell;
}
}
Its quite simple with UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (dataObjects.count == 0){
return 0;
}
return dataObjects.count;
}

How do I show multiple custom cells using objective C?

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;

Tableview cells only displaying correctly on scroll up

I'm struggling with creating a tableview with custom, subclassed cells created in storyboards.
All the cells work well except the "questionCell". This cell only shows a fragment of its textfield (or sometimes none of it) until I scroll down and then back up again.
The initial appearance:
After scrolling down then up again:
Where am I going wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"TextCell";
static NSString *CellIdentifier2 = #"QuestionCell";
static NSString *CellIdentifier3 = #"ImageCell";
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
UITableViewCell *cell = nil;
if (section == 0){
//the intro section
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
textCell.labelText.text = _intro;
cell = textCell;
} else if (section == 4){
//this is the "the answer is . . ." section
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
textCell.labelText.text = [NSString stringWithFormat:#"The only EOM to not be eliminated is %#",_theAnswer];
cell = textCell;
} else {
if (row == 0) {
NSString *item = [NSString stringWithFormat:#"question_%li", (long) section];
NSString *txt = [self valueForKey: item];
QuestionCell *qCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
qCell.questionText.text = txt;
cell = qCell;
} else if (row == 1) {
NSString *item = [NSString stringWithFormat:#"img_%li", (long)section];
NSString *img = [self valueForKey: item];
ImageCell *iCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3 forIndexPath:indexPath];
iCell.image.image = [UIImage imageNamed: img];
cell = iCell;
} else {
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
NSString *item = [NSString stringWithFormat:#"explain_%li", (long)section];
NSString *txt = [self valueForKey: item];
textCell.labelText.text = txt;
cell = textCell;
}
}
return cell;
}
Here's heightForRowAtIndexPath. The results are the same if I just comment this out and set the heights in the prototype cells.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// Return NO if you do not want the specified item to be editable.
CGFloat hgt = 70;
if (indexPath.section == 0){
//the intro
hgt = 150;
} else if (indexPath.section == 4){
//the "answer is . . ."
hgt = 100;
} else {
if (indexPath.row == 1) {
//the image
hgt = 225;
} else if (indexPath.row == 2){
//the discussion
hgt = 180;
}
}
return hgt;
}
the questionCell just sets the iboutlet:
#import <UIKit/UIKit.h>
#interface QuestionCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextView *questionText;
#end

How can I solve this delegate error?

I have a delegate, but it doesn't work without the other delegate line in it:
- (id)initWithData:(NSData *)data delegate:(id <ParseOperationDelegate>)theDelegate {
self = [super init];
if (self != nil) {
self.dataToParse = data;
self.delegate = theDelegate;
}
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
It should load data in a Cell but the data isn't shown before the first scroll. What can I do?
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"productCell";
static NSString *PlaceholderCellIdentifier = #"placeholderCell";
int nodeCount = [appDelegate.products count];
if (nodeCount == 0 && indexPath.row == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PlaceholderCellIdentifier];
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.detailTextLabel.text = #"Loading...";
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (nodeCount > 0) {
XMLProductView *listedProduct = [appDelegate.products objectAtIndex:indexPath.row];
cell.textLabel.text = listedProduct.name;
// cell.detailTextLabel.text = appRecord.artist;
if (!listedProduct.productImage) {
if (self.tableView.dragging == NO && self.tableView.decelerating == NO) {
[self startImageDownload:listedProduct forIndexPath:indexPath];
}
UIImageView *listedImage = (UIImageView *)[cell viewWithTag:1];
listedImage.image = [UIImage imageNamed:#"Placeholder.png"];
}
else {
UIImageView *listedImage = (UIImageView *)[cell viewWithTag:1];
listedImage.image = listedProduct.productImage;
}
}
return cell;
}
your code seems that its waiting for some event, or some files to get fetched or downloaded, so you will need to call [tableview reloadData]; when this data is downloaded,
I cant figure it out from the code, but my guts tells me this
1st:
that you are waiting for data from int nodeCount = [appDelegate.products count]; to be ready, so you will need to have some sort of delegate that gets called when this data is ready
2nd:
you are waiting for an image to get downloaded here [self startImageDownload:listedProduct forIndexPath:indexPath], so when this actually get downloaded you will need to set the image to that cell or reloadData on the table
That is what i can figure out from the code.

Resources