I have a UITableView which is populated with an NSArray. The array is sorted alphabetically in my table with alphabetized headers.
I am adding a custom cell randomly in my table at every 10th row.
The issue I have is that the custom cell is layering over actually data, so it's not being inserting within the array.
How can I solve this?
Here's what I am doing to insert the custom cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 10 == 10-1) {
CustomAdCell *cell = (CustomAdCell *)[tableView dequeueReusableCellWithIdentifier:#"AdCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomAdCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
else {
static NSString *cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Configure the cell...
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *key = [[[self indexedMembers] allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)][indexPath.section];
self.indexedMembersArray = ((NSMutableArray *)[self indexedMembers][key]);
MemberInfo *currentMember = self.indexedMembersArray[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currentMember.firstName, currentMember.lastName];
return cell;
}
I need to use two UITableView in one UITableViewController. And I need to create two custom cell for each UITableView.
Can I possible to make this? please help me.
try this.....used two UITableView in one UIViewController...
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==self.tableview1)
{
return tableview1RowCount;
}
else if(tableView==self.tableview2)
{
return tableview2RowCount;
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.tableview1)
{
static NSString *CellIdentifier1 = #"tableview1_cell";
tableview1_cell *cell1 = (tableview1_cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tableview1_cell" owner:self options:nil];
cell1 = [nib objectAtIndex:0];
}
return cell1;
}
if (tableView==self.tableview2)
{
static NSString *CellIdentifier1 = #"tableview2_cell";
tableview2_cell *cell2 = (tableview2_cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tableview2_cell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
return cell2;
}
}
and for the method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
do same as above....separate tableviews with if condition and give actions to each...whatever u want...
friends,
i want to add more than one customised cell in a table view in xib file.
In detail i want to add different cells for each row in table i tried the below code but seems to be not working i can only customise one cell for all the rows in tableview code is as below i have tried two codes
code1 is as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
identifier=#"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.name.text=#"gopi";
return cell;
if (indexPath.row==1)
{
NSString *identifier1;
identifier1=#"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
cell1.gopilabel.text=#"sabari";
return cell1;
}
}
It displays only one customised cell so i tried this code code2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if (indexPath.row==0)
{
identifier=#"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.name.text=#"gopi";
return cell;
}
else if(indexPath.row==1)
{
identifier=#"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
cell1.gopilabel.text=#"sabari";
return cell1;
}
return nil;
}
i know this statement is wrong but i do not know what to return here if i have is it possible to customise more than one cell for each row
Update your code as below. Check for even and odd cell and put customize cell in Your table view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if (indexPath.row %2 == 0)
{
identifier=#"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.name.text=#"gopi";
return cell;
}
else if(indexPath.row %2 == 1)
{
identifier=#"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
cell1.gopilabel.text=#"sabari";
return cell1;
}
return nil;
}
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 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.