UITableview double delete button issue - ios

We are using an uitableview and allow users to delete uitableviewcells, but a strange bug occurs, where two delete buttons appear.
Has anyone seen this before or have any ideas how to resolve this issue?
Thank you for your help and time.
Regards
Jing Jing Tao
UPDATE 1
Code for my cell at index for row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
//cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
UPDATE 2
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
//Sections logic
Model *info;
switch ([indexPath section])
{
case 0:
info = [self.bapiArray objectAtIndex:[indexPath row]];
break;
case 1:
info = [self.tablesArray objectAtIndex:[indexPath row]];
break;
case 2:
info = [self.tcodesArray objectAtIndex:[indexPath row]];
break;
default:
break;
}
cell.textLabel.text = info.subsection;
cell.detailTextLabel.text = info.text;
if(!(info.program == (id)[NSNull null] || info.program.length == 0))
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# Program:%#", info.text,info.program];
}
}

Related

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

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 to load two tableview in same view controller with cell for index row?

i am facing one module consists to load two different UITableView in same UIViewController i know where i doing mistake, the problem is cell for row AtIndexpath in table view method. i getting only one UITableView in UIViewController but my secondviewcontroller not return any values in cell.
here my sample code for ur reference:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==table) {
return [self.arryData count];
}
else
return [tblArr count];
NSLog(#"tabeCount==>%lu",(unsigned long)tblArr.count);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *CellIdentifier1 = #"Cell";
dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell;
mobilePlanDetailsCellTableViewCell *plan_cell;
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
}return cell;
if (tableView==planTable) {
plan_cell = [self.planTable dequeueReusableCellWithIdentifier:CellIdentifier1];
if (plan_cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
plan_cell = [nib objectAtIndex:0];
}
plan_cell.label.text = [[tblArr objectAtIndex:indexPath.row] objectForKey:#"Answer1"];
plan_cell.Label2.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer2"];
plan_cell.Label3.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer3"];
plan_cell.label4.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer4"];
}
return plan_cell;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView setBackgroundColor:[UIColor whiteColor]];
[cell setBackgroundColor:[UIColor whiteColor]];
}
you made the small mistake just change the Return Cell in inside the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
if (tableView==table) {
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
return cell;
}
else {
mobilePlanDetailsCellTableViewCell *plan_cell = [self.planTable dequeueReusableCellWithIdentifier: CellIdentifier];
if (plan_cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
plan_cell = [nib objectAtIndex:0];
}
plan_cell.label.text = [[tblArr objectAtIndex:indexPath.row] objectForKey:#"Answer1"];
plan_cell.Label2.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer2"];
plan_cell.Label3.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer3"];
plan_cell.label4.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer4"];
return plan_cell;
}
}
The problem is that you are returning the cell outside the if (tableView==table), so your program will NOT reach the code after it.
Instead of
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
}return cell; //This line is wrong
You should do this:
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
//Should be inside the if scope
return cell;
}
Why you are doing such complex coding, Just simply provide different tag value for Table Views, and you are returning cell outside of your if condition which is wrong. Follow the below code.
Do a checking like this:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.tag==1)// Table 1
{
//load cell as per your choice
//cell operations
return cell;
}
else // Table 2
{
//load cell as per your choice
//cell operations
return cell;
}

JSON partial string

How can i return the part of a string in a cell in Xcode?
I have a json responce:
{"Email":"john#appleseed.com","Password":"4321","lijst":"eieren, kaas, melk, 2 uien, brood(volkoren)"}
how can i get the value's at "lijst" in different cells in xcode?
So cell 1 = Eieren
cell 2 = Kaas
etc...
this is the code to display it all in one cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
switch (indexPath.row) {
case 0:
cell.textLabel.text = [self name];
cell.detailTextLabel.text = #"Name";
break;
case 1: {
NSString *email = [details objectForKey:#"Email"];
if (!email)
email = #"No email";
if ([email isKindOfClass:[NSArray class]])
email = #"<Multiple emails>";
cell.textLabel.text = email;
cell.detailTextLabel.text = #"Email";
break;
}
case 2:
cell.textLabel.text = self.details[#"lijst"];
cell.detailTextLabel.text = #"List";
break;
default:
break;
}
return cell;
}
It appears that you have parsed this JSON into a dictionary called self.details. You could then, as #bdesham pointed out, get an array of the individual values with componentsSeparatedByString:. For example, if you had a NSArray property called lijstArray, you could do something like:
NSString *lijstString = self.details[#"lijst"];
self.lijstArray = [lijstString componentsSeparatedByString:#", "];
Then, lijstArray[0] will have the first value, lijstArray[1] the second, etc. The number of values is [lijstArray count].
The above only works if you know that there will be exactly one and only one space after the commas. If you don't know that for sure, you'd just use #"," for the separator, but then trim the whitespace. For example, if you declared the lijstArray property to be a NSMutableArray, you could do something like:
NSString *lijstString = self.details[#"lijst"];
NSArray *tempArray = [lijstString componentsSeparatedByString:#","];
self.lijstArray = [NSMutableArray array];
for (NSString *lijstValue in tempArray) {
[self.lijstArray addObject:[lijstValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
Whichever approach you adopt, having populated an array, lijstArray, you can now use that in your cellForRowAtIndexPath. For example, let's assume your tableview had two sections, section 0 being the name and email, and section 1 being the individual lijstArray values, e.g.:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return 2;
else if (section == 1)
return [self.lijstArray count];
return 0; // we never should get here, but in the spirit of defensive programming, let's make sure we handle this
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = [self name];
cell.detailTextLabel.text = #"Name";
break;
case 1: {
NSString *email = [details objectForKey:#"Email"];
if (!email)
email = #"No email";
if ([email isKindOfClass:[NSArray class]])
email = #"<Multiple emails>";
cell.textLabel.text = email;
cell.detailTextLabel.text = #"Email";
break;
}
default:
break;
}
} else if (indexPath.section == 1) {
cell.textLabel.text = self.lijstArray[indexPath.row];
}
return cell;
}

Populating UITableViewCell with labels

Okay so here's the issue...
I have a UITableView that has 3 sections and each section has a different amount of cells.
How would I insert an array(s) of labels that would go in the appropriate section and table..?
I can't figure this one out.
Solved:
- (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];
}
NSString *labelText;
switch (indexPath.section) {
case 0:
labelText = [_section1 objectAtIndex:[indexPath row]];
break;
case 1:
labelText = [_section2 objectAtIndex:[indexPath row]];
break;
case 2:
labelText = [_section3 objectAtIndex:[indexPath row]];
break;
}
cell.textLabel.text = labelText;
return cell;
}

Resources