- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"reviews";
static NSString *CellIdentifier1 = #"recipes";
static NSString *CellIdentifier2 = #"easy_tips";
// Configure Cell
NSString *type = [[myaray objectAtIndex:indexPath.row]objectForKey:#"type"];
NSLog(#"types====%#",type);
UITableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (type == CellIdentifier) {
firstCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(#"First type %#", type);
return cell;
}else if (type == CellIdentifier1){
SecondTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
return cell1;
}else if (type == CellIdentifier2){
ThirdTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
return cell2;
}
return mycell;
}
I am getting only one cell every time it is first cell .
It is not going to if condition statement.
Thanks in advance for your reply
Change every part you use == to compare string to isEqualToString:
For example:
Change
if (type == CellIdentifier)
To
if([type isEqualToString:CellIdentifier])
Try using Switch Condition rather If and else if
I think it would be better if you consider rethinking the whole logic.
I would do the same thing like this
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *type = [[myaray objectAtIndex:indexPath.row]objectForKey:#"type"];
myCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CustomCellid" ];
if(myCell==nil)
{
NSArray *arr= [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
if(types isEqualToString:#"reviews")
{
myCell=[arr objectAtIndex:0];
}
else if(types isEqualToString:#"recipes")
{
myCell=[arr objectAtIndex:1];
}
else if(types isEqualToString:#"easy_tips")
{
myCell=[arr objectAtIndex:2];
}
}
return myCell;
}
Create a UITableViewCell class 'CustomCell'.
Create three UITableViewCells in 'CustomCell.xib'. Each cell for each type, the whole xib will be having the identifier 'CustomCellid'
Then the above code would give you desired result. give a try
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 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 have issue, my app crash on cellForRowAtIdexPath because I want to add 2 different custom table view cells for 2 different rows.
See my code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"MatchDetail";
MatchDetailsCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
static NSString *CellIdentifier2 = #"GoalsList";
GoalsListCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
if (cell1 == nil) {
cell1 = (MatchDetailsCell *)[MatchDetailsCell cellFromNibNamed:#"MatchDetailsCell"];
}
[cell1 setDataInCell:arrAllGames :strTeamA :strTeamB];
return cell1;
}
else if (indexPath.row == 1)
{
if (cell2 == nil) {
cell2 = (GoalsListCell *) [GoalsListCell cellFromNibNamed:#"GoalsListCell"];
}
[cell2 setDataInCell:arrGoalsList :[arrAllGames count]];
return cell2;
}
}
return nil;
}
Your code is incorrect. You must dequeue the cell of this row with this identifier.
If you have only one section in the table, you don't need to do the indexPath.section check. Try this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"MatchDetail";
static NSString *CellIdentifier2 = #"GoalsList";
if(indexPath.row == 0) {
//The first row is the MatchDetailsCell
MatchDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell == nil) {
//If cell not exists, you must create a new one
cell = [[MatchDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
//Rest of your cell code here
return cell;
} else {
//Rest of the cells are GoalsListCell
GoalsListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell == nil) {
cell = [[GoalsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
//Rest of your cell code here
return cell;
}
}
I don't have more information about your code, but maybe it helps you.
Try that, you shouldn't deque the cell first and after that you load it from nib.
If it doesn't work post crash log.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"MatchDetail";
static NSString *CellIdentifier2 = #"GoalsList";
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
MatchDetailsCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
[cell1 setDataInCell:arrAllGames :strTeamA :strTeamB];
return cell1;
}
else if (indexPath.row == 1)
{
GoalsListCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
[cell2 setDataInCell:arrGoalsList :[arrAllGames count]];
return cell2;
}
}
return nil;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexPath.row%2)==0) // for even rows, like 1,3,5,...
{
MyFirstCell *firstCell = (MyFirstCell *)[tableView dequeueReusableCellWithIdentifier:#"MyFirstCell"];
if (firstCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyFirstCell" owner:self options:nil];
firstCell = (MyFirstCell *)[nib objectAtIndex:0];
}
return firstCell;
}
else // for even rows, like 2,4,6,...
{
MySecondCell *secondCell = (MySecondCell *)[tableView dequeueReusableCellWithIdentifier:#"MySecondCell"];
if (secondCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MySecondCell" owner:self options:nil];
secondCell = (MySecondCell *)[nib objectAtIndex:0];
}
return secondCell;
}
return nil;
}
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.