load 2 tableviews in a single view iOS [closed] - ios

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have 2 tableviews (table1,table2) in a view controller. I have 2 data arrays (dataArray1, dataArray2).
I want to load the table views with corresponding data arrays i.e.(table1 = dataArray1, table2 = dataArray2).I am trying the below code. But app is crashed? What's wrong in this code? Any help would be appreciated. Please don't suggest to use 2 view controllers.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.table1)
return [dataArray1 count];
if(tableView == self.table2)
return [dataArray2 count];
}
- (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];
}
if(tableView == self.table1)
{
cell.textLabel.text =[dataArray1 objectAtIndex:indexPath.row];
}
if(tableView == self.table2)
{
cell.textLabel.text =[dataArray2 objectAtIndex:indexPath.row];
}
return cell;
}

Normally you dont create two table view in one view.
But rather create a sections.
Even then if u still need two table view its better to use View Class and make separate delegates for them.
I have one code
https://github.com/bishalg/BGRadioList
Which shows you the use of view with table view.
Hope it is helpful to you.

Provide 2 different cell identifiers for the two tableviews
Because both table cells are different and they should be reused ,for maintainig the reuse correctly unique identifier requires to be seperate
Try this
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell;
if(tableView == self.table1)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier1];
}
cell.textLabel.text =[dataArray1 objectAtIndex:indexPath.row];
}
if(tableView == self.table2)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier2];
}
cell.textLabel.text =[dataArray2 objectAtIndex:indexPath.row];
}
return cell;
}

Related

Working with dynamic and static sections together in a tableview

I am having some problem to manage my cells in a tableview.
I have one table view with two sections:
The first one, I have 3 custom cells, with static cells.
The second one, I have a dynamic type.
To work with the dynamic one, no problem is occurring, but the static, I don't know how to "reuse" them.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.section = 0){
//here is the question, how to return the stactis that had already been defined
}else{
return cell //here is okay, i can return the dynamic
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.section = 0){
//here is the question, how to return the stactis that had already been defined
static NSString *CellIdentifier = #"StaticCell";
StaticCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}else{
static NSString *CellIdentifier = #"DynamicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}

UITableViewCell subtitle not appearing on load

I have a table with cells where the subtitle only appears after something has been searched. This is the code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(tableView == self.tableView)
{
// Configure the cell...
cell.textLabel.text = self.objects[indexPath.row];
cell.detailTextLabel.text = #"test2";
}
else{
// Configure the cell...
cell.textLabel.text = self.results[indexPath.row];
cell.detailTextLabel.text = #"test1";
}
return cell;
}
EDIT: ArtOfWarfare's answer was correct. it was a problem in the storyboard. See image below:
Your code suggests that you have two separate tables... I would suggest that your problem lies within the differences in how the tables have been configured, possibly in your Storyboard.
Do you, perchance, have one of them set with a prototype with an identifier of "Cell" but a style that isn't UITableViewCellStyleSubtitle? That would make it so that your cell creation method isn't called in one, but only in the other.

Adding "search for" cell at the end of search view results like Instagram

I have a search filtering through an array, which works well. However, I want to add an extra cell at the end that basically says "search for %#" where %# is the search query. When this cell is clicked, it will load a new table view from a php query.
From what I understand I'm probably going to new 2 prototype cells, with one segueing into the new table view and the other into a view controller for results.
However, every time I try anything I'm met with a mess of exceptions. Can someone provide guidance?
Right now my cellForRowAtIndexPath delegate looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [filteredPeople objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [followers objectAtIndex:indexPath.row];
}
return cell;
}

call different custom cell in each UISegmentController

I have one UIViewController with UITableView inside,above table I have UISegmentControl, when I press on segment control I want to load a UItableCustomeCell, would you please help me in this implementation, I don't know how should I add them in cellForRowAtIndexPath, Since I have 3 different Custom cell
Here is the code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
if (indexPath.row == self.segmentedControl.selectedSegmentIndex == Test1) {
MytestsCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MytestsCell"];
if (!cell) {
cell = [[MyBooksCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"MytestsCell"];
}
return cell;
}
else if (indexPath.row == self.segmentedControl.selectedSegmentIndex == tests) {
testCell *cell = [tableView dequeueReusableCellWithIdentifier:#"testCell"];
if (!cell) {
cell = [[TestsCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"testsCell"];
}
return cell;
}
break;
case 1:
if (indexPath.row == self.segmentedControl.selectedSegmentIndex == PTest) {
PTestsCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PTestsCell"];
if (!cell) {
cell = [[PTestsCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"PTestsCell"];
}
return cell;
}
break;
}
I don't want to have 3 of them in one table, each custom cell is for one segment control
Thanks in advance!
One alternative I can think of is to switch the table views data source. But I would not recommend that. You could define a delegate of your data source and ask it for the table view cell for a selected segmented control. But this just moves the problem. I would stick to your approach.
So...here is what I would do. Starting with iOS6, you no longer need to check if your cell is nil after dequeuing from the tableview if you use
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
You are guaranteed to get a cell back as long as the identifier exists. Also, it doesn't look like you need to do any additional configuration so something like this should work:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:#"%d", self.segmentedControl.selectedSegmentIndex];
return [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
}
Edit: I forgot to add, that in order to use this, use numbers that correspond to the segments as the identifier for each cell.

Objective C - iOS Multiple Table Views

I'm trying to write an app that connects to my web service and displays data in a table view. I also have a static table view for a menu. The static table view works (responds to clicks, shows options, etc.) but I'm stumped on getting the second table view working. There is no more than 1 table view per window. I'll be adding about 10 table views.
Code for static table view:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"dataSelect"; //Name of table view
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
tableData is the array that I'm inserting.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"dataSelect"; //Name of table view
if (tableView == tableview1) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
} else if (tableview == tableview2) {
// do actions related to tableview 2 here
} else {
// and so on
}
}

Resources