IOS: delegate methods for two tableView - ios

myaI have this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == firstTableView){
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ;
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
}
I check if tableview is firsttableview, but it give me a warning because the method haven't a "return" cell, how can I solve?

You only return a cell if the table is firstTableView. Make sure you return a cell for other tables by adding a return statement outside of your conditional.

Your code needs to return a value for all paths through that method. So, if your check for firstTableView fails, you still need to return a valid UITableViewCell from the method. You should probably read the UITableView programming guide - it walks you through proper usage of a tableview.

Related

I uitableview the (cell = nil) always not call?

in the ui tableview cell,
the statement if (cell == nil) is not called.
Because when i initialize the cell outside the if statement, the talbe will have some wrong label in wrong row.
how can I initialize my cell?
The #"tttestttt" NS LOG is not all the time when the table is created.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"GroupsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
NSLog(#"tttesttttt");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
}
Use the more convenient method dequeueReusableCellWithIdentifier:forIndexPath: which returns always a valid cell.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"GroupsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier
forIndexPath:indexPath];
...
return cell;
}
dequeueReusableCellWithIdentifier: always return a cell if you're using Storyboard.
make Identifier not a fixed value
make it as index.row (convert to string as identity)
your wrong may be crate by cell refuse

UITableView "cellForRowAtIndexPath" not working

I'm trying to use "reloadData" method on a uitableView, it seems like it works (both dataSource method are being called) and when I debug "cellForRowAtIndexPath" the cell that it returns is the correct cell) but I can't see the cell (numberOfRowsInSection is changing and adding blank space for each new cell - So it's not seems to be a threads problem). For example, if I have in the tableView 5 "names" (my data array called "namesArray") and I add 2 there will be 5 names and 2 nil cells presented on the tableView. The problem seems to be with cellForRowAtIndexPath.
cellForRowAtIndex:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creating "cellIdentifier"(NSString) with "Cell" as the value
NSString *cellIdentifier=#"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
//Creating a default cell using "cellIdentifier"(NSString)
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[namesArray objectAtIndex:indexPath.row];
//Returning cell
return cell;
}
numberOfRowsInSection:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.namesArray.count;
}
-(void)addNameToNamesArray:(NSString*)name
{
[self.namesArray addObject:name];
NSLog(#"namesArray lastObject: %#",[self.namesArray lastObject]);
//NSLog print: The correct name (the one that was added)
[self.tableView reloadData];
}
screenshot for "5 names and 2 nil cells":
Do you have any idea what can it be?
You call
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
inside cellForRowAtIndexPath!
Don't you think that's a bit late? And if it worked, you would be calling it a bit often?
And if dequeue... returns nil when a class is registered, something is badly wrong.
change to
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creating "cellIdentifier"(NSString) with "Cell" as the value
static NSString *cellIdentifier=#"Cell";
//[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
//Creating a default cell using "cellIdentifier"(NSString)
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[namesArray objectAtIndex:indexPath.row];
//Returning cell
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.namesArray.count;
}
set the outlet of tableview and then in addNameToNamesArray method after adding elements reload the tableview
This is likely because you have updated the data in your array, but the table view hasn't accessed that data yet, leading to the nil cells that you see. Since your cells load properly with the initial 5 element in your array, it's probable that you simply haven't refreshed the table view's data. Try using [tableView reloadData] after you have added the new elements to your array.
EDIT 1
What about changing your cellForRowAtIndexPath code to this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Creating "cellIdentifier"(NSString) with "Cell" as the value
NSString *cellIdentifier=#"Cell";
//Creating a default cell using "cellIdentifier"(NSString)
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textLabel.text = [self.namesArray objectAtIndex:indexPath.row];
NSLog(#"name: %#", cell.textLabel.text);
//Returning cell
return cell;
}
Let us know the output of NSLog.
1.Make sure you have initialized self.namesArray in viewDidLoad
Try the following:
[tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:cellIdentifier];
like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];
return cell;
}
Your code is perfectly alright. There can be some possibilities that its not working for you.
You are not initialising your datasource i.e NSMutableArray in your case.
It depends how you are calling addNameToNamesArray method.
If you can share your whole code for that class as how you are calling method and every delegates you are using.

Parse custom tableview cell

I am trying to make a custom tableview cell for an iPad app, using the Parse framework. When i query without the custom cell, it works fine, but as soon as i am changing the cell identifier, i am just stuck at the loading icon.
The code goes as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"specielCelle";
sagerCelle *cell = (sagerCelle *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[sagerCelle alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell
cell.lblPolice.text = [object objectForKey:#"police"];
//cell.textLabel.text = [object objectForKey:self.textKey];
//cell.imageView.file = [object objectForKey:self.imageKey];
return cell;
}
Here my advices,(By the way the question is not clear)
Firstly change your identifier with an indexpath.Row
NSString *cellIdentifier=[NSString stringWithFormat:#"CELL_%d",(int)indexPath.row];
Second
if you are changing your existing cell then you are not be able to go inside of the block of
if(cell==nil)
because your cell is not nil. Write else condition.
Hope it helps.

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;
}

cellForRowAtIndexPath not being called?

I am working on a project where I need to populate the cells in a table and have them show on the screen. I've got the table to show but now data is being shown in the cells. I've tried using breakpoints and found that this method never gets called. The method just before it (numberOfRowsInSection) does get called. Any suggestions?
- (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];
}
Resident *aResident = (Resident *)[appDelegate.residents objectAtIndex:indexPath.row];
cell.textLabel.text = aResident.name;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Have you set the delegate and datasource for the TableView ?
Yeah, numberOfRowsInSection probably returns 0. Show it and where you populate your data source.

Resources