I have 3 tableViews in one view and for some reason two of them are showing data but one isn't, what's my issue?!!
I'm guessing the issue is in the cellForRowAtIndexPath declaration. Attached it is.
- (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];
}
if (tableView==_titleTV) {
NSDictionary *dictionaryTwo = [_titleArray objectAtIndex:indexPath.section];
NSArray *arrayTwo = [dictionaryTwo objectForKey:#"data"];
NSString *cellValueTwo = [arrayTwo objectAtIndex:indexPath.row];
cell.textLabel.text = cellValueTwo;
}
if (tableView==_cuisineTV) {
NSDictionary *dictionary = [_cuisineArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"data"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
else {
int storyIndex = [indexPath indexAtPosition: [indexPath length]-1];
cell.textLabel.text=[_favouritesArray objectAtIndex: storyIndex];
}
return cell;
}
What's the problem??
Thanks,
SebOH.
Try printing out the value of cellValueTwo to ensure its not a blank string:
NSLog(#"cellValueTwo value: %#",cellValueTwo);
Did you check to make sure the number of cells being created for _titleTV is correct with what you expect from - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section?
Related
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;
}
I want to get cell value in tableView by using:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(#"selected cell: %#", selectedCell);
}
It should be a String but this is what i get in the NSLog:
selected cell: <RewardCategoriesTableViewCell: 0x7ae11290; baseClass = UITableViewCell; frame = (0 92.01; 320 44); autoresize = W; layer = <CALayer: 0x7ae11450>>
And this is my Data Source of Array:
NSArray* data= [[NSArray alloc]initWithObjects:category1, category2, category3, category4, category5, category6, category7, nil];
return data;
This is cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"RewardCategoriesTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RewardCategoriesTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.section >= [categories count]){
return nil;
}
Categories *category = [categories objectAtIndex:indexPath.section];
NSString *identifier = [NSString stringWithFormat:#"Cell%ld", indexPath.section];
NSLog(#"%#" , identifier);
cell.lblCategory.text = category.CategoryName;
NSLog(#"cell value: %#", cell.lblCategory.text);
return cell;
}
Thanks for helps.
In your way, you get the UITableviewCell or subclass
object,so what you log is right.
If you want to get cell value,you should check your dataSource,in
MVC pattern that is your Model.
For example: you have a array named dataArray as Model,Then you get
value dataArray[indexPath.row]
I am not sure why you set text of cell based on the indexPath.section,so ,I post example code based on the code you post
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Categories *category = [categories objectAtIndex:indexPath.section];
NSString * cellText = category.CategoryName;
}
BTY,I think the right way is let text of cell based on the row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*) [tableView dequeueReusableCellWithIdentifier:#"RewardCategoriesTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RewardCategoriesTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.row>= [categories count]){
return nil;
}
Categories *category = [categories objectAtIndex:indexPath.row];
NSString *identifier = [NSString stringWithFormat:#"Cell%ld", indexPath.row];
NSLog(#"%#" , identifier);
cell.lblCategory.text = category.CategoryName;
NSLog(#"cell value: %#", cell.lblCategory.text);
return cell;
}
Then in:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Categories *category = [categories objectAtIndex:indexPath.row];
NSString * cellText = category.CategoryName;
}
I've got the code below to fill up an array of notifications. This array is needed to fill up a menu in my iOS app with notifications.
// extract specific value...
NSArray *switchValues = [res objectForKey:#"data"];
NSLog(#"%#", switchValues);
for (NSDictionary *switchValue in switchValues) {
NSString *text = [switchValue objectForKey:#"text"];
NSLog(#"%#", text);
[self.notificationsArray addObject:text];
}
Further down the line I then do 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];
}
NSMutableArray *titles = self.notificationsArray;
cell.textLabel.text = titles[indexPath.row];
return cell;
}
However, I keep getting an empty array. What am I doing wrong?
Why don't you do
[notificationsArray objectAtIndex:indexPath.row]
instead of
NSMutableArray *titles = self.notificationsArray;
cell.textLabel.text = titles[indexPath.row];
?
If you really dont want, at least do
NSMutableArray *titles = [NSMutableArray arrayWithArray:self.notificationsArray];
And in the first piece of code, what are the Console Logs? Are you sure you enter in the for loop?
I need to create a Grouped UITableView like showing in the below picture. I created a Grouped UITableView in XIB and added the relevant code as per below code.
It's a simple grouped UITableview.
I like to know, how can i add On/OFF Switch like Button (Pls refer the image Security header) only those first two cells and remaining will be UITableViewCellAccessoryDisclosureIndicator?
Could someone guide me on this please?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Do any additional setup after loading the view from its nib.
NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:#"Quick Logon",#"Stay Logged On", nil];
NSArray *arrTemp2 = [[NSArray alloc]initWithObjects:#"Notifications",#"Text Messaging",#"Family Members",#"Social Media",nil];
NSArray *arrTemp3 = [[NSArray alloc]initWithObjects:#"Payment Accounts",nil];
NSArray *arrTemp4 = [[NSArray alloc]initWithObjects:#"Phone Nickname",#"Version",nil];
NSDictionary *temp =[[NSDictionary alloc]initWithObjectsAndKeys:arrTemp1,#"Security",arrTemp2,#"Profile & Preferences", arrTemp3,#"Payment Preferences", arrTemp4,#"About", nil];
self.tableContents =temp;
NSLog(#"table %#",self.tableContents);
NSLog(#"table with Keys %#",[self.tableContents allKeys]);
self.sortedKeys =[[self.tableContents allKeys] sortedArrayUsingSelector:#selector(compare:)];
NSLog(#"sorted %#",self.sortedKeys);
}
#pragma mark Table Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.sortedKeys count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.sortedKeys objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
Add On/OFF Switch to the cell propotype and show/hide when it needed.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
cell.switch.hidden = indexPath.secton > 0;
cell.accessoryType = indexPath.secton > 0 ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
return cell;
}
You should create two different prototype cells in your xib with two different identifiers. In cellForRowAtIndexPath:, dequeue the one you want based on the section,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
static NSString *SwitchCellIdentifier = #"SwitchCellIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:SwitchCellIdentifier];
// configure the cell;
return cell;
}else{
cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
// configure the cell;
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.