The command cell.textLabel.text and cell.detailTextLabel.text is providing output in console but it does not give any value on the table view on simulator...
This is the code for generating the cell i have used..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"bookCell";
UITableViewCell *cell;
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// Book *bookDetail=[booksArrayDataSource objectAtIndex:indexPath.row];
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text= [self titleForRow:indexPath];
NSLog(#"title for row= %#", cell.textLabel.text);
cell.detailTextLabel.text= [self subtitleForRow:indexPath];
NSLog(#"subtitle for row= %#",cell.detailTextLabel.text);
return cell;
}
go to your storyboard/xib and change the cell type from dynamic to subtitle.
whatever you have here, will not be executed:
if (cell == nil)
{
...
}
If your loading string value in cell use following statement:
cell.textLabel.text =[[YOURArray_Name objectAtIndex:indexPath.row] stringValue]; (or)
cell.textLabel.text =[[YOURArray_Name objectAtIndex:indexPath.row] description];
If your loading integer value in cell use following statement:
cell.textLabel.text =[[YOURArray_Name objectAtIndex:indexPath.row] integerValue];
If your loading Float value in cell use following statement:
cell.textLabel.text =[[YOURArray_Name objectAtIndex:indexPath.row] floatValue];
Related
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
}
This is a code that i got from github and crashes on the lines i commented, i know EXC BAD ACCESS is when you dont retain or release something but i have ARC enabled so i dont know what to do.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
CountryCell *cell = (CountryCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[CountryCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[_dataRows objectAtIndex:indexPath.row] valueForKey:kCountryName]; //here crashes
cell.detailTextLabel.text = [[_dataRows objectAtIndex:indexPath.row] valueForKey:kCountryCallingCode]; //here crashes
return cell;
}
I Think one of the best way to set text property in label
cell.textLabel.text = [NSString stringWithFormat:#"%#",[[_dataRows objectAtIndex:indexPath.row] valueForKey:kCountryName]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#",[[_dataRows objectAtIndex:indexPath.row] valueForKey:kCountryCallingCode]];
its may be useful ..
I assign some custom view to UITableViewCell.accessoryView, but if I scroll the tableView crazy, some accessoryView will disappear in iOS 7, and if I touch the cell, it's accessoryView can appear, I don't know why, because it's correct in iOS 6. This is my code, someone can help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView = [dict objectForKey:#"switch"];
}
else
{
cell.accessoryView = nil;
}
return cell;
}
When we use ReusableCellWithIdentifier in table view it reuse cells in table, you set cell.accessoryView = nil; it's remove accessory view for all cells in table view with same CellIdentifier try this code, it's solve your problem :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryView = [dict objectForKey:#"switch"];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView.hidden=NO;
}
else
{
cell.accessoryView.hidden=YES;
}
return cell;
}
I Have an NSMutableArray which is being displayed as
(
13002,
My Drive,
13006,
Testing1,
13007,
Testing123
)
In my NSLog
I want to populate my UITableView with just the names (My Drive, Testing1 & Testing123) and want to use the ID's as the subtitle.
use cell for row at indexpath method and number of rows method as below
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count/2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int row = [indexPath row]*2;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
}
cell.textLabel.text = [array objectAtIndex:row+1];
cell.detailTextLabel.text = [array objectAtIndex:row];
return cell;
}
your problem will solve...
NSmutableArray *element; //contains your array
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
element = [array objectAtIndex:indexPath.row];
cell.textLabel.text = element;
element = [array objectAtIndex:indexPath.row +1 ];
cell.subtitle.text = element;
Depending on your application, you might want to consider using a NSMutableDictionary instead of the array. This way, your data model will accurately represent the fact that those names are mapped to ids. You can use
NSArray *arrayOf Keys = [theDictionary allKeys];
to get an array of the keys in your dictionary, then do something like this:
cell.textLabel.text = [theDictionary objectForKey:[arrayOfKeys objectAtIndex:indexPath.row]];
cell.detailtextLabel.text = [arrayOfKeys objectAtIndex:indexPath.row];
You should also consider using the free Sensible TableView framework. You would just pass the array to the framework and it will automatically display it in the table view.
So far search on Stack Overflow I havent found a situation that is like mine. Any assistance is greatly appreciated: I keep seeing that if I put a checkmark on Person A, Person H will also have one as well as does a person about 10 away. Basically abut every 10 it repeats a check mark.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text =
[NSString stringWithFormat:#"%# %#", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"FirstName"],[[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"LastName"]];
cell.detailTextLabel.text =
[NSString stringWithFormat:#"%#", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"Address"]];
return cell;
}
In my did select row for index path I have this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
cell = [self.tableView cellForRowAtIndexPath: indexPath];
if ([[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"emailSelected"] != #"YES")
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:#"YES" forKey:#"emailSelected"];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:#"NO" forKey:#"emailSelected"];
}
This is due to how UITableView "recycles" UITableViewCell for efficiency purposes, and how you are marking your cells when they are selected.
You need to refresh/set the accessoryType value for every cell you process/create within tableView:cellForRowAtIndexPath:. You properly update the state in your myArrayOfAddressBooks data structure, and you just need to use this information in tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *info = [myArrayOfAddressBooks objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", [info objectForKey:#"FirstName"],[info objectForKey:#"LastName"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [info objectForKey:#"Address"]];
cell.accessoryType = ([[info objectForKey:#"emailSelected"] isEqualString:#"YES"]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
Also, unless there is a good reason for saving the state as #"Yes" or #"No" strings, why not save them as [NSNumber numberWithBool:YES] or [NSNumber numberWithBool:NO]? This will simplify your logic when you want to do comparisons versus having to use isEqualToString: all the time.
e.g.
cell.accessoryType = ([[info objectForKey:#"emailSelected"] boolValue]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;