I am new to UITableViews, and I dont know very much about this topic.
I have two buttons in the same View, but not in the Table, for control the switch between the UITableViewCells, when one button is pressed then set a boolean for load the specific Cell.
If you click the button 1 then it shows the table with the CellType1.
If you click the button 2 then it shows the table with the CellType2.
My goal is use the same Table for this. But I dont know where to start.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
if (clickButton1 == true)
{
clickButton1 *cell = (clickButton1*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellType1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = #"Trial";
return cell;
}
else if (clickButton2 == true)
{
clickButton2 *cell = (clickButton2*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellType2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//cell.name.text = #"Trial";
return cell;
} else
I think that this code is not the best. But I am lost at this point.
Thanks in advance!
using a tutorial example like this...
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tweetsArray count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//5
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
if you have two arrays for items such as self.tweetArray1 and self.tweetArray2 you could change this code to
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int i = buttonNumberClicked;
return [self.tweetsArray[i] count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
//5
int i = buttonNumberClicked;
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray[i] objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
and set up ibactions for your buttons to change the variable/int buttonNumberClicked to 1 or 2 depending on which was pressed and [tableView reloadData]; inside ibaction as well, hope this helps.
Related
I'm trying to get my custom .XIB Cell called "ProfileCell1" into the first cell of my table view (size 150) and then my second custom .XIB Cell called "ProfileCell2" into the second cell (size 60).
I can get "ProfileCell1" into the first row, but I can't figure out how to put the second one in, since the first will repeat when I increase the return value from 1 on the "numberOfRowsInSection".
Here is my code so far:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 150;
}
So I know this code repeats my ProfileCell1, but I just can't figure out how to make it appear only once, and then start my ProfileCell2 from the second cell, and have both cells return 2 different values in the 'heightForRowAtIndexPath.
I've tried stuff like:
if (indexPath.row == 0) {
return 150;
else {
return 60;
}
but it doesn't seem to work. Cheers!
try this way
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) {
return 1;// your require number of cell
}
else
{
return 1;
}
}
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
else
{
// your second cell coding here
static NSString *identifier = #"ProfileCell2";
ProfileCell2 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// your second cell code
//cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
}
and increase height for cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
return 150;
}
else
{
return 60;
}
}
you can use two different cell in same UITableView using two section. first section use first cell .xib and second section use second .xib files.
You can also use two different cells in single section. Making some edit to Dharmesh Dhorajiya's Post.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0) {
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
else
{
// your second cell coding here
static NSString *identifier = #"ProfileCell2";
ProfileCell2 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
}
And use same code you are using to return height for two different cells.
if (indexPath.row == 0) {
return 150;
else {
return 60;
}
How about identifying the indexPath where you want to put ProfileCell1 and ProfileCell2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// say you want to use ProfileCell1 in first row
if(indexPath.row == 0) {
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
// cell properties
return cell;
} else {
static NSString *identifier = #"ProfileCell2";
ProfileCell2 *cell = (ProfileCell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];
// cell properties
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return 150;
else
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
static NSString *identifier = #"ProfileCell1";
ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
//bla bla bla code for ProfileCell1 here
return cell;
}
else
{
static NSString *identifier = #"ProfileCell2";
ProfileCell2 *cell = (ProfileCell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];
//bla bla bla code for ProfileCell2 here
return cell;
}
}
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;
}
I want to divide the cells in groups as per condition but i am getting repeated values where i am wrong.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
return 1;
}
else if(section == 1)
{
return 6;
}
else if(section ==2)
{
return 2;
}
else
{
return [arrControls count]-9;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [arrControls objectAtIndex:indexPath.row];
return cell;
}
Use an array of NSMutableArrays for each section.
NSMutableArray *arrays[5];
for (int i = 0; i < 5;i++)
{
arrays[i] = [[NSMutableArray alloc]init];
}
[arrays[0] addObject:#"add Your objects in section 0"];
[arrays[1] addObject:#"add Your objects in section 1"];
[arrays[2] addObject:#"add Your objects in section 2"];
etc..
Then.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
array[section].count
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [array[indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
inside your cellForRowAtIndexPath , u need to return different cells for different section
Let say u have the 3 custom classes( with inferface for all of them ) inherited from UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(inderPath.Section ==0 )
{
//assuing that custom class name is detailTableViewCell.h and interface file name is same .
static NSString *simpleTableIdentifier = #detailTableViewCell ;
detailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]
if(cell == nil )
{
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:#"detailTableViewCell" ownder self options:nil];
cell = [nib objectAtIndex:0]
}
return cell ;
}
if(inderPath.Section ==1 )
{
similarly for another subclass of UItableViewCell ;
}
if(inderPath.Section ==2 )
{
similarly for another subclass of UItableViewCell ;
}
// one common cell for other sections
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [arrControls objectAtIndex:indexPath.row];
return cell;
}
try this`-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(indexPath.section == 0)
{
cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1)
{
cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 2)
{
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 3)
{
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [FourthArray objectAtIndex:indexPath.row];
}
return cell;
}
`
#Dalvik. You need different array for each section. And you already have only one mutableArray.
You can take the array where each object in array will give you array for the rows.
The format of data you can use is as follows
(
{key : Array},
{key : Array},
{key : Array},
{key : Array},
)
or simply
(
(Array),
(Array),
)
inside, numberOfRowsInSection: you can access it using
{
return [[[dataArray objectAtIndex:section]ObjectFoyKey:key]count];
or
return [[dataarray objectAtIndex:section]count];
}
inside, cellFOrRowAtIndexPath: you can access it using
{
array = [[dataArray objectAtIndex:section]ObjectFoyKey:key];
cell.textlabel.text = [array objectAtIndex:indexPath.row];
or
array = [dataArray objectAtIndex:section];
cell.textlabel.text = [array objectAtIndex:indexPath.row];
}
Hope this will help you..
Please elaborate your question a little bit. Its hard to understand what exactly your problem is.
In case you are using xib or storyboard for tableCell use this approach 1:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCellIdentifier";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (MyCell *)[nib objectAtIndex:0];
}
In case you are not using storyboard for cell try this approach 2:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCellIdentifier";
UITableViewCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setNeedsDisplay];
}
Hope this will help you..
i'm trying to use two UITableView in one ViewController. one UITableView with the reference of custom cell and the other one is simple...I've written this code but it gives me error of control may reach end of non-void function...
so give me the suggestion for it...what can i do...?
thanks in advance...
here, is my code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.categoryTable)
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell1)
{
cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell1.textLabel.text=[category objectAtIndex:indexPath.row];
cell1.textLabel.highlightedTextColor = [UIColor redColor];
return cell1;
}
else if (tableView==self.listTable)
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
//NSLog(#"%ld",(long)indexxxx);
//NSLog(#"%lu",(unsigned long)imagesmarry.count );
cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row];
//NSLog(#"%#",[imagesmarry_pictitle objectAtIndex:indexxxx]);
return cell2;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView==self.categoryTable)
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell1)
{
cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell1.textLabel.text=[category objectAtIndex:indexPath.row];
cell1.textLabel.highlightedTextColor = [UIColor redColor];
cell=cell1;
}
else if (tableView==self.listTable)
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
//NSLog(#"%ld",(long)indexxxx);
//NSLog(#"%lu",(unsigned long)imagesmarry.count );
cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row];
//NSLog(#"%#",[imagesmarry_pictitle objectAtIndex:indexxxx]);
cell=cell2;
}
return cell;
}
You can lose the second if condition
if (tableView==self.categoryTable)
{
//do stuff
return cell1;
}
else
{
//do stuff
return cell2;
}
try this, you have to init the type the cell as UITableviewViewCell and cast after. Tell me if this sample answer to your question.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView==self.categoryTable) {
static NSString *cellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[category objectAtIndex:indexPath.row];
cell.textLabel.highlightedTextColor = [UIColor redColor];
} else if (tableView==self.listTable) {
static NSString *simpleTableIdentifier = #"SimpleTableCell";
cell = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = (RWTListCell *)[nib objectAtIndex:0];
}
//NSLog(#"%ld",(long)indexxxx);
//NSLog(#"%lu",(unsigned long)imagesmarry.count );
((RWTListCell *) cell).textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row];
//NSLog(#"%#",[imagesmarry_pictitle objectAtIndex:indexxxx]);
}
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (tableView==self.categoryTable) {
...
...
return cell1;
}
else if (tableView==self.listTable) {
...
...
return cell2;
}
retrun cell;
}
Try this, take a dummy cell and return it at the end.
Actually your problem is not returning any cell to tableview.
I have altered your code. I just removed your else if condition
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.categoryTable)
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell1)
{
cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell1.textLabel.text=[category objectAtIndex:indexPath.row];
cell1.textLabel.highlightedTextColor = [UIColor redColor];
return cell1;
}
static NSString *simpleTableIdentifier = #"SimpleTableCell";
RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
//NSLog(#"%ld",(long)indexxxx);
//NSLog(#"%lu",(unsigned long)imagesmarry.count );
cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row];
//NSLog(#"%#",[imagesmarry_pictitle objectAtIndex:indexxxx]);
return cell2;
}
Just add return; at the last.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (condition 1) {
// statements
return cellType_1;
} else if (condition 2) {
//statements
return cellType_2;
}
return;
}
or you can also do this -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (condition 1) {
// statements
return cellType_1;
} else {
//statements
return cellType_2;
}
}
remove the second condition . because you must return in non void method weather your all condition is true or not.
I am having trouble inputting my string "handle" into a group UITableView that I have;
for(NSDictionary * dataDict in servers) {
NSString * handle [dataDict objectForKey:#"handle"];
}
I'm totally confused on how I could utilize this for statement to dynamically create new cells with the handle.
The actual UITableView is on the main view controller and doesn't have any cells. How can I loop through the values and create the cells with labels?
You've to overrite this datasource methods for your UITableView, don't forget to set datasource for the table. Just reload your data once you fill your server array, [table reloadData];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return servers.count; //will create cell based on your array 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];
}
//show handle stirng from server array
cell.textlabel.text = [[server objectAtIndex:indexPath.row]objectForKey:#"handle"];
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
mycustomcell *cell = (mycustomcell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"mycustomcell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog( #"NIB = %#",nib);
NSLog( #"Cell = %#",cell);
}
cell.lblName.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Name"];
cell.lblId.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Id"];
cell.lblGender.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Gender"];
cell.lblAddress.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Address"];
cell.lblPhone.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Phone"];
cell.lblEmail.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Email"];
NSLog(#"tbl %#",cell.lblName.text);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
here mycustomcell is Custome cell not tableview's cell... I hope this works for you.. Create new custome cell with named mycustomecell with .h , .m and .xib..
You should use the method below available on UITableViewDataSource protocol.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath