How To Change UITableView Cell Height Equally & Show detailedTextLabel? - uitableview

Hi I have this code here.
- (void)viewDidLoad
{
[super viewDidLoad];
jsonURL = [NSURL URLWithString:#"http://oo.mu/json2.php"];
jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL usedEncoding:nil error:nil];
self.jsonArray = [jsonData JSONValue];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [jsonArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] * 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[self.jsonArray objectAtIndex:indexPath.row] objectForKey:#"Name"];
return cell;
}
What I want is:
To change the height of the UITableView cell so I can fit more
things under the textLabel. (Currently, when I use the previous
code to increase the height of the UITableView cell, each cell
goes from big to small in different sizes).
To show under the textLabel.text a detailedTextLabel of
something else under it.
How would I do that?
I tried:
cell.textLabel.text = [[self.jsonArray objectAtIndex:indexPath.row] objectForKey:#"Name"];
cell.detailTextLabel.text = [[self.jsonArray objectAtIndex:indexPath.row] objectForKey:#"Street"];
cell.detailTextLabel.text = [[self.jsonArray objectAtIndex:indexPath.row] objectForKey:#"City"];
But it doesn't show up.

You can create new UItableview cell with the dimensions as per your requirements. Later
-(Uitableview) cellForIndexPath {
//add the customized Uitableviewcell.
}
In this way you can create the tableview cell as per your requirements. I hope it will help.

Related

iOS: Adding on/off control for only first two cells in the Grouped UITableView

I need to create a Grouped UITableView like showing in the below picture. I created a Grouped UITableView in XIB and added the relevant code as per below code.
It's a simple grouped UITableview.
I like to know, how can i add On/OFF Switch like Button (Pls refer the image Security header) only those first two cells and remaining will be UITableViewCellAccessoryDisclosureIndicator?
Could someone guide me on this please?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Do any additional setup after loading the view from its nib.
NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:#"Quick Logon",#"Stay Logged On", nil];
NSArray *arrTemp2 = [[NSArray alloc]initWithObjects:#"Notifications",#"Text Messaging",#"Family Members",#"Social Media",nil];
NSArray *arrTemp3 = [[NSArray alloc]initWithObjects:#"Payment Accounts",nil];
NSArray *arrTemp4 = [[NSArray alloc]initWithObjects:#"Phone Nickname",#"Version",nil];
NSDictionary *temp =[[NSDictionary alloc]initWithObjectsAndKeys:arrTemp1,#"Security",arrTemp2,#"Profile & Preferences", arrTemp3,#"Payment Preferences", arrTemp4,#"About", nil];
self.tableContents =temp;
NSLog(#"table %#",self.tableContents);
NSLog(#"table with Keys %#",[self.tableContents allKeys]);
self.sortedKeys =[[self.tableContents allKeys] sortedArrayUsingSelector:#selector(compare:)];
NSLog(#"sorted %#",self.sortedKeys);
}
#pragma mark Table Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.sortedKeys count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.sortedKeys objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
Add On/OFF Switch to the cell propotype and show/hide when it needed.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
cell.switch.hidden = indexPath.secton > 0;
cell.accessoryType = indexPath.secton > 0 ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
return cell;
}
You should create two different prototype cells in your xib with two different identifiers. In cellForRowAtIndexPath:, dequeue the one you want based on the section,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
static NSString *SwitchCellIdentifier = #"SwitchCellIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:SwitchCellIdentifier];
// configure the cell;
return cell;
}else{
cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
// configure the cell;
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;
}

Selecting Cell in Table IOS

I am trying to create a method that changes the string object "tableColorName" to the cell selected. The tableData NSArray consists of object: "red","blue","green". I want to save the string "tableColorName" to redColor if red is selected, blueColor if blue, greenColor if green. After the cell is selected I want the viewController to go back to the root. I appreciate your help in advance:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int theRow = indexPath.row;
NSString *tableColorName;
tableColorName = [[NSString alloc] initWithString:([_tableData [theRow] stringValue],#"Color")];
[self.navigationController popToRootViewControllerAnimated:YES];
}
//first of all take one NSArray and
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.colorNames = [[NSArray alloc] initWithObjects:#"Red", #"Green",
#"Blue", #"Indigo", #"Violet", nil];
}
// Implement Table method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.colorNames count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell.
self.navigationItem.title=#"Colors";
UIImage *cellImage = [UIImage imageNamed:#"a.png"];
cell.imageView.image = cellImage;
NSString *colorString = [self.colorNames objectAtIndex: [indexPath row]];
cell.textLabel.text = colorString;
NSString *subtitle = [NSString stringWithString: #"All about the color "];
subtitle = [subtitle stringByAppendingFormat:colorString];
cell.detailTextLabel.text = subtitle;
return cell;
}
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
int idx = indexPath.row;
obj.lbl.text=[#"You select "stringByAppendingString:[colorNames objectAtIndex:idx]];
[self popToViewController animated:YES];
}
Try this ::
NSArray *arr;
NSString *tableColorName; // Use in AppDelegate
- (void)viewDidLoad
{
arr = [[NSArray alloc] initWithObjects:#"Red", #"Green", #"Blue", nil];
}
Table View Methods ::
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.title.text = [NSString stringWithFormat:#"%#", [arr objectAtIndex:indexPath.row]];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
app.tableColorName = [NSString StringWithFormat:#"%# Color", [arr objectAtIndex:indexPath.row]];
[self.navigationController popToRootViewControllerAnimated:YES];
}
Then, access by app.tableColorName whenever you want to display.
Thanks.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//do whatever with the selected cell.
//go back to the root
}

Data Lost after scroll in UITableView

I am trying to display plist data in tableview.
My problem is that after scrolling down i lost my previously fetched data.
Actually i am using custom cell so i think this is happen.
Code for Source and delegate of table is below.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = #"Cell";
OffersCustomCell *cell = (OffersCustomCell *)[mytableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"OffersList" ofType:#"plist"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"OffersCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.titleLabel.font = [UIFont fontWithName:#"Formata-Regular" size:13.0f];
cell.saveLabel.font = [UIFont fontWithName:#"Formata-Bold" size:14.0f];
cell.nowLabel.font = cell.titleLabel.font;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *dict = [tableData objectAtIndex:indexPath.row];
NSLog(#"%#",dict);
cell.titleLabel.text = [dict objectForKey:#"title"];
cell.nowLabel.text = [dict objectForKey:#"price"];
cell.saveLabel.text = [dict objectForKey:#"rondel"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[mytableView deselectRowAtIndexPath:indexPath animated:YES];
}
Did you set the height for each cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 0;
//calculate height for each cell
return height;
}
Use this UITableView delegate method to set height of the cells.
you can use another class that is store your data and reload time you used that class and restore your data correctly and efficiently.
I solved it. :)
only problem is no retaining memory for UItableview.
tableData = [[NSArray arrayWithObjects:[dic objectForKey:#"Response"], nil] retain];

Resources