array objects loading but not connecting to cell - ios

I'm passing an array of favorites -- from a details view -- to (what should be) a table view displaying the favorites individually.
The first array [in the first view] is okay:
Array found. Contents: (
"\U5df4",
"\U5df4",
"\U82ad\U8305//\U5df4\U8305",
"\U5df4\U5bb6",
"\U7b06\U7bd3\U513f",
"\U5df4",
"\U5df4",
"\U5df4",
"\U5df4",
"\U5df4",
"\U5df4\U5bb6",
"\U5df4",
"\U5df4",
"\U5df4"
)
Number of items in my array is: 15
and printing it [from the second array in the second view]:
Printing description of self->FavsTwo:
<__NSCFArray 0x1f9644a0>(
巴,
巴,
芭茅//巴茅,
巴家,
笆篓儿,
巴,
巴,
巴,
巴,
巴,
巴家,
巴,
巴,
巴,
巴家
)
is also cool...
The first break i get is here:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [FavsTwo count];
}
and then all these errors:
*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:6235
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
***
my cell looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *cellValue = [self.FavsTwo objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
trying to convert objects to strings -- not really sure where it's failing
cheers

cellForRowAtIndexPath return nil, if can not ReusableCell, you should create a cell with theCellIdentifier
try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *cellValue = [self.FavsTwo objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}

You are missing self in the tableView:numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.FavsTwo count];
}
The dequeueReusableCellWithIdentifier: may return nil if it can not deque a cell.
You need to check if the cell is nil:
- (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]
}
NSString *cellValue = [self.FavsTwo objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
If you have used registerClass:forCellReuseIdentifier: or registerNib:forCellReuseIdentifier:.The you need to get the cell with dequeueReusableCellWithIdentifier:forIndexPath::
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *cellValue = [self.FavsTwo objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}

Related

Randomly Grouping TableView data in xcode

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..

selected UITableViewCell data is null?

Table is showing the list of countries. But when i tried to retrieve the data its returning null value. I am trying store the cell.textlabel.text into a string but worrying with null
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CellIdentifier"];
}
jsonDict = [newarray objectAtIndex:indexPath.row];
cell.textLabel.text = [jsonDict objectForKey:#"countryName"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"country is %#",cell.textLabel.text);
cell.textLabel.text=CountryString;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
jsonDict = [newarray objectAtIndex:indexPath.row];
CountryString=[jsonDict objectForKey:#"countryName"]
NSLog(#"country is %#", CountryString);
}
your cell identifier also different
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell"; //here mention the correct one use either `MyCell` or `CellIdentifier`
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
jsonDict = [newarray objectAtIndex:indexPath.row];
cell.textLabel.text = [jsonDict objectForKey:#"countryName"];
return cell;
}

*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]

I got the above error when trying to launch my app second time. First time I got it properly. But when I try to select the tableview, I am getting the error. please help me
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier =CELL_IDENTIFIER_STRING;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.textLabel.text = [arrayname objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.textColor=UIColorFromRGB(0xffffff);
return cell;
}
- (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] autorelease];
}
////// *****************************
cell.textLabel.text = [arrayname objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.textColor=UIColorFromRGB(0xffffff);
return cell;
}

Looping through JSON array and inputting string into grouped UITableView

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

UITableView array returns nil

Right now I have a tableview, and I can assure you that the delegate method all work. But I am trying to load the tableview from an array. the same array returns actual objects when i log then earlier in the app, but when I try to access the same array in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
it always returns nil/null.Here is the implementation of - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:
NSLog(#"cell for row at twitter called");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tweet = [timeLineData objectAtIndex:indexPath.row];
// NSLog(#"Tweet at index %d is %#", [indexPath row], tweet);
//NSLog(#"Tweet: %#", tweet);
cell.textLabel.text = [tweet objectForKey:#"text"];
cell.detailTextLabel.text = [tweet valueForKeyPath:#"user.name"];
return cell;
I am so confused as to why this is, because in all of my other projects the same code works just fine. Any help would be greatly appreciated.
Thanks,
Virindh Borra
Would it help if you access the array using:
[yourArray objectAtIndex:indexpath.row];

Resources