selected UITableViewCell data is null? - ios

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

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

UITableViewCell.accessoryView will disappear in iOS 7

I assign some custom view to UITableViewCell.accessoryView, but if I scroll the tableView crazy, some accessoryView will disappear in iOS 7, and if I touch the cell, it's accessoryView can appear, I don't know why, because it's correct in iOS 6. This is my code, someone can help me?
- (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 = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView = [dict objectForKey:#"switch"];
}
else
{
cell.accessoryView = nil;
}
return cell;
}
When we use ReusableCellWithIdentifier in table view it reuse cells in table, you set cell.accessoryView = nil; it's remove accessory view for all cells in table view with same CellIdentifier try this code, it's solve your problem :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryView = [dict objectForKey:#"switch"];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView.hidden=NO;
}
else
{
cell.accessoryView.hidden=YES;
}
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

populating a tableview with nsmutabledata received from a webservice

I would like to populate a tableview with a NSMutableData that received from a webservice. I can get the data and display at several components but cannot fill the tableview because following code doesn't accept NSMutableData ;
AnyNSarray = [NSPropertyListSerialization propertyListWithData: webData options: 0 format:&plf error: &error];
//webData is NSMutableData that i populate with webservice data and AnyNSarray is a NSArray to provide tableview at
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Here is tableview populating blocks (also following code works at view loads but is not fired again after i click button):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"cell%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.text = [birnsarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = #"any details";
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return AnyNSarray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [birnsarray count];
}
Hey in numberOfRows function you are returning AnyNSarray count, so replace your cellForRowAtIndexPath with the below code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"cell%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.text = [AnyNSarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = #"any details";
}
return cell;
}

Resources