Two tableviews in one view doesn't work - objective-c

I have two UITableviews in my view, delegates and datasources of these two table are connected to File's Owner. I have two IBOutles connected to my UITableView:
IBOutlet UITableView *tableOne;
IBOutlet UITableView *tableTwo;
In my case, the information contained in tableOne are being passed to tableTwo, I believe the problem is inside the method cellForRowAtIndexPath as you can see below:
-(void)viewDidAppear:(BOOL)animated{
[tableOne reloadData];
[tableTwo reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == tableOne) {
return 3;
}else{//Else is the tableTwo
return 3;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == tableOne) {
static NSString *simpleTableIdentifier = #"ContasAlertaTableViewCell";
ContasAlertaTableViewCell *cell = (ContasAlertaTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContasAlertaTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.data.text = [date objectAtIndex:indexPath.row];
return cell;
}else{// Else is tableTwo
static NSString *simpleTableIdentifier = #"FaltasTableViewCell";
FaltasTableViewCell *cell = (FaltasTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"FaltasTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.data.text = [date2 objectAtIndex:indexPath.row];
return cell;
}
}
If you notice I'm using different table cells for each of the tables, and I believe that would be what is influencing this error, in my case, What I have to do to solve this type of problem?

Related

iOS:Selection and saving data or print the data when row is selected in table view

Iam using the did select method to select the data .so data in a particular row is selecting and aim getting the check mark also now my problem is when the check mark is on state the data should be printed in console and if its off the data should be removed from the array. my code is as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(#"array is %#",array);
static NSString *CustomCellID = #"cell";
contactcellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellID];
if (cell == nil)
{
cell=[[contactcellTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellID];
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"contactcellTableViewCell" owner:self options:nil];
cell =[nib objectAtIndex:0];
}
cell.Firstnamelbl.text = [[array objectAtIndex:indexPath.row] objectForKey:#"first_name"];
cell.Lastnamelbl.text=[[array objectAtIndex:indexPath.row]objectForKey:#"last_name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[array removeAllObjects];
contactcellTableViewCell *cell = (contactcellTableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
static NSString *CustomCellID = #"cell";
if (cell == nil)
{
cell=[[contactcellTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellID];
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"contactcellTableViewCell" owner:self options:nil];
cell =[nib objectAtIndex:0];
}
if (cell.m_checkImageView.image == [UIImage imageNamed:#"Selected.png"])
cell.m_checkImageView.image = [UIImage imageNamed:#"Unselected.png"];
else
cell.m_checkImageView.image = [UIImage imageNamed:#"Selected.png"];
}
I had two labels in cell so that when the particular row is selected the data should be print on the console
i got the answer please find below
if (cell.m_checkImageView.image == [UIImage imageNamed:#"Selected.png"])
{
cell.m_checkImageView.image = [UIImage imageNamed:#"Unselected.png"];
}
else
{
cell.m_checkImageView.image = [UIImage imageNamed:#"Selected.png"];
NSLog(#"string is %#",string);
}
}
now we will see the data when row is selected

Expand / Collapse UITableViewCell with different cell and data source

I did a UITableView filled with a plist data source, and I did custom cells with Interface Builder (so I'm using xib files)
here's the part of code where I'm creating cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"DataTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if ([view isKindOfClass:[UITableViewCell class]]) {
cell = (DataTableViewCell*)view;
}
}
}
return cell;
}
then when I use the:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method to get the selected cell, I call this method to expand the cell:
- (void)expandCellAtIndex:(int)index {
NSMutableArray *path = [NSMutableArray new];
NSArray *currentCells = [subCellItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentCells count]; i++) {
[path addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Now the problem is that since I've not done this before, I'm stuck on the logic on how to change the cell I want to show when expanded, that's because the method:
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
is searching for the right path and inserting a cell with the same cell xib file as the one I've loaded with my data at the start
how can I set a different cell (with a different xib, and different data) to show instead of the same as before?
basically I have this table working but in the expanding cell I see a copy of the cell you touch
You can do it like this:
In .h:
#interface TSViewController : UIViewController
{
NSMutableArray* subCellIndexPaths;
}
In DataTableViewSubCell.xib create one "Table View Cell" (other Views you have to remove). Change Class for "Custom Class" to DataTableViewSubCell. Replay it for second cell.
In .m:
- (UITableViewCell*) tableView: (UITableView*) tableView
cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
static NSString *CellIdentifier = #"Cell";
DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
if ([subCellIndexPaths containsObject: indexPath])
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed: #"DataTableViewSuperCell"
owner: nil
options: nil];
cell = [nib objectAtIndex: 0];
}
else
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed: #"DataTableViewSubCell"
owner: nil
options: nil];
cell = [nib objectAtIndex: 0];
}
}
return cell;
}
- (void) expandCellAtIndex: (int)index
{
NSMutableArray* indexPaths = [NSMutableArray new];
NSArray* currentCells = [subCellItems objectAtIndex: index];
int insertPos = index + 1;
for (int i = 0; i < [currentCells count]; i++)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow: insertPos++
inSection: 0];
[indexPaths addObject: indexPath];
[subCellIndexPaths addObject: indexPath];
}
[self.tableView insertRowsAtIndexPaths: indexPaths
withRowAnimation: UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: index
inSection: 0]
atScrollPosition: UITableViewScrollPositionTop
animated: YES];
}
And of course you must create subCellIndexPaths anywhere (in init or viewDodLoad methods).

Table view doesn't show up custom cells

I have a view controller that shows up a table view controller when a button is clicked. Everything works fine with table view delegates, table view is shown fine, but in ellForRowAtIndexPath: delegate method, cell is instantiated and returned, but it's not shown correctly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}
Many thanks
Why are you creating your cell like this?
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If it is your own custom cell why do you use UITableViewCellStyleDefault ?
If the cell you are loading is a subclass of UITableViewCell and you've used the interface builder to build the cell you have to do a couple of things.
In the nib file delete the view that is there when you create it, add a UITableViewCell and change it's class to alrededorCell. Changes the cells class and not the file's owner. When you are linking buttons,labels etc . be sure to link them to the cell not to file's owner. Also set the cells uniqueIdentifier to alrededor.
In cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:#"nibName" owner:nil options:nil];
for (alrededorCell *view in xib) {
if ([view isKindOfClass:[alrededorCell class]]) {
cell = view;
}
}
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}

Receive Data from UITableViewCell

I have the following code set:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:#"CustomCellView" owner:nil options:nil];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
for(id currentObj in nibObjs)
{
if([currentObj isKindOfClass:[CustomCell class]])
{
cell = (CustomCell *)currentObj;
}
}
}
AssessmentDetail * anAssess = [module.assessmentDetails4 objectAtIndex:indexPath.row];
[[cell labelAssessment] setText:anAssess.assessmentName4];
return cell;
}
In my custom cell there is a UISlider. What I would like to do is use a button to retrieve the value of each UISlider from each row so I can get a total value.
I thought about doing this but I'm not certain on where to go from there:
- (IBAction) calculateTotal:(id)sender {
static NSString *CellIdentifier = #"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
Thanks for the help.
Cycle through the subviews of the UITableView:
- (IBAction) calculateTotal:(id)sender {
NSArray *subViews = [myTableView subviews];
float total;
for (int i = 0; i < subViews.count; i++)
{
id obj = [subViews objectAtIndex:i];
if ([obj isKindOfClass:[CustomCell class]])
{
total += [[obj getMySlider] getValue];
}
}
// do something with total
}

select custom UITableViewCell by Identifier from xib

I try to fill my UITableView withd different UITableViewCells. I tried to choose 1 of 3 UITableViewCells out of a XIB-file by identifier but I can't get the value of the UITableViewCell's Identifier.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = (MyTableViewCell *) [tableView dequeueReusableCellWithIdentifier:#"CellTwo"];
if(nil == cell){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyTableViewCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[MyTableViewCell class]]) {
MyTableViewCell *tmpCell = (MyTableViewCell *) currentObject;
if (tmpCell.identifier == #"CellTwo") {
cell = (MyTableViewCell *) currentObject;
}
[tmpCell release];
break;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;}
I solved this exact problem by creating a subclass for each UITableViewCell. When you do your comparison of the class type, you can put in the type of cell it is:
cell = (FooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"footerTableViewCell"];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CommonTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[FooterTableViewCell class]]){
cell = (FooterTableViewCell *)currentObject;
return cell;
}
}
}
I have some tables that reuse the exact same kind of cell formatting and i wanted to have them in a common XIB that i can alter at anytime. In the .h for that controller, i simply declared three sub classes of UITableViewCell for each of the three cell types.