how to manage cellForRowAtindexPath Value for TableViewIndexSearch my code is :
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
alphabetDict = [NSDictionary dictionaryWithObject:tempArray forKey:#"alphabet"];
NSLog(#"alphabetDict Is: %#", alphabetDict);
[listArray addObject:alphabetDict];
NSLog(#"listArray Is: %#", listArray);
return tempArray;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 26;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)] autorelease];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, -5, headerView.frame.size.width-120.0, headerView.frame.size.height)];
for(section ; section <=[self.tempArray count];section++)
{
headerLabel.textAlignment = UITextAlignmentLeft;
headerLabel.text = [self.tempArray objectAtIndex:section];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont fontWithName:#"Verdana Bold" size:11];
[headerView setBackgroundColor:[UIColor orangeColor]];
// NSLog(#"headerLabel.text:%#", headerLabel.text);
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
return headerView;
}
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 23.0;
}
// --------- TableView Delegate and DataSource-----------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [xmlParseArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DirectoryCustomCell *directoryCustomCell =(DirectoryCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (directoryCustomCell == nil) {
directoryCustomCell = [[[DirectoryCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
directoryCustomCell.selectionStyle = UITableViewCellEditingStyleNone;
}
UIImageView *searchImg = [[UIImageView alloc]initWithFrame:CGRectMake(292, 0, 28, 360)];
searchImg.image = [UIImage imageNamed:#"searchindex.png"];
[directoryCustomCell addSubview:searchImg];
NSString *companyName = [[xmlParseArray objectAtIndex:indexPath.row ]objectForKey:#"firstname"];
// NSLog(#"companyName :%#",companyName);
NSString *flName = [[xmlParseArray objectAtIndex:indexPath.row ]objectForKey:#"title"];
[directoryCustomCell setDataForEvent1:companyName venue:flName];
return directoryCustomCell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[collation sectionTitles] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [collation sectionForSectionIndexTitleAtIndex:index];
}
I want to show in sorted form (I mean section headertitle A have the name start from A and so on).
Between Headertitle from A to B these data are common in every section but I want only those starts from A in SectionHeadertitle A and starts from B in headertitle B.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch(indexPath.section)
{
// Configure based on section
// Get details corresponding to section
}
// configure cell and return cell
}
Take a look at this tutorial
Related
I am not able to add a new row in a UITableview,the code is as follows,
it showing an error with
-(void)viewDidLoad
{
self.title=#"CHECK-list";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addItem)];
self.navigationItem.leftBarButtonItem = addButton;
ar=[[NSMutableArray alloc]initWithObjects:#"Map",#"Camera",#"First Aid Kit", nil];
[super viewDidLoad];
}
- (void)addItem
{
//NSMutableArray *tempArray = [[NSMutableArray alloc] init];
int i = 0;
for (NSArray *count in ar)
{
[ar addObject:[NSIndexPath indexPathForRow:i++ inSection:1]];
}
[[self tableView] beginUpdates];
[[self tableView] insertRowsAtIndexPaths:(NSArray *)ar withRowAnimation:UITableViewRowAnimationNone];
[[self tableView] endUpdates];
[self.tableView reloadData];
}
Here Fazil.... basic code for any tableview and the row addition
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(160, 280, 150, 150) style:UITableViewStylePlain];
tableview.delegate = self;
tableview.dataSource = self;
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier ];
}
cell.textLabel.text= [array objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_normal.png"]];
cell.textLabel.textColor = [UIColor blackColor];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
return cell;
}
#pragma mark UItableView delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
check = indexPath.row;
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
theCell.contentView.backgroundColor=[UIColor clearColor];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{
[tableview moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:[array count] - 1 inSection:1]];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
NSInteger row = 0;
if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
}
return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];
}
return proposedDestinationIndexPath;
}
I'm facing a crazy issue which is about expanding and shrinking UITableViewCell. When I click just one cell it works properly but while one cell remains open, clicking another cell to open causes the cell disorder. I would want to expand and shrink at the same time.
Here is my source.
- (void)viewDidLoad {
[super viewDidLoad];
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
//Add items
[listOfItems addObject:#"1"];
[listOfItems addObject:#"2"];
[listOfItems addObject:#"3"];
[listOfItems addObject:#"4"];
[listOfItems addObject:#"5"];
[listOfItems addObject:#"6"];
[listOfItems addObject:#"7"];
[listOfItems addObject:#"8"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listOfItems count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *aLabel; UILabel *bLabel; UILabel *v1Label; UILabel *v2Label;; UIView *v1; UIView *v2;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#" cell null");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
aLabel = [[[UILabel alloc] initWithFrame:CGRectMake(9.0, 8.0, 50.0, 20.0)] autorelease];
aLabel.tag = 1;
aLabel.font = [UIFont systemFontOfSize:30];
[cell.contentView addSubview:aLabel];
v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 116)];
v1.backgroundColor = [UIColor redColor];
v1.tag = 10;
v1.hidden = YES;
[cell.contentView addSubview:v1];
[v1 release];
v2 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 100)];
v2.backgroundColor = [UIColor blueColor];
v2.tag = 11;
v2.hidden = YES;
[cell.contentView addSubview:v2];
[v2 release];
}
else {
aLabel = (UILabel *)[cell.contentView viewWithTag:1];
v1 = (UIView *) [cell.contentView viewWithTag:10];
v2 = (UIView *) [cell.contentView viewWithTag:11];
}
aLabel.text = [listOfItems objectAtIndex:indexPath.row];
if (SelectedIndexPath == indexPath.row)
{
if ([aLabel.text intValue] % 2) {
v1.hidden = NO;
v2.hidden = YES;
}
else {
v1.hidden = YES;
v2.hidden = NO;
}
}
else {
v1.hidden = YES;
v2.hidden = YES;
}
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (SelectedIndexPath == indexPath.row)
{
return 100.0;
}
else {
return 46.0;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (SelectedIndexPath == -1)
{
OldSelectedIndexPath = indexPath.row;
SelectedIndexPath = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:NO];
}
else
{
if (SelectedIndexPath == indexPath.row)
{
OldSelectedIndexPath = SelectedIndexPath;
SelectedIndexPath = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:NO];
}
else
{
SelectedIndexPath = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:OldSelectedIndexPath inSection:indexPath.section], indexPath, nil] withRowAnimation:NO];
OldSelectedIndexPath = SelectedIndexPath;
}
}
}
The problem comes from the facth that your cell will have a height of 160.0 when selected.
Look here
v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 116)];
You give an heigh of 116 to a view which y origin is 44. Thus your cell view will have a total height of 160.0
Change the value returned in
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
To match the correct total height of your cell.
The problem is in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Or may be the problem is due to releasing. anything is going to release and you are still using it.
first comment // all release and inter prate your
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This is the code i have:
- (id)init
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
//self.tableView.delegate = self;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor *wood = [UIColor colorWithPatternImage:[UIImage imageNamed:#"wood_pattern.png"]];
self.view.backgroundColor = wood;
self.tableView.separatorColor = [UIColor clearColor];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (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];
cell.backgroundView.backgroundColor = [UIColor redColor];
}
// Configure the cell...
cell.textLabel.text = #"Test";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60;
}
When i scroll down and then back up to a cell which was previously on screen, the app crashes. Any ideas why?
On a side note.. the background color is set to red, and although i can see the text, I can't see the background color.
Bind UITableView properly in XIB. and right following code.
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.separatorColor = [UIColor clearColor];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (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.contentView.backgroundColor = [UIColor redColor];
cell.textLabel.text = #"Test";
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell.
return cell;
}
My app shows this in the 4.3.1 simulator and this in the 5.0 simulator.
This is the code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"";
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [[Singleton sharedInstance] getTitleSectionView:segmentedControl.selectedSegmentIndex inDay:section];
}
-(UIView *)getTitleSectionView:(int)week inDay:(int)day;
{
UILabel *lab=[[[UILabel alloc]init]autorelease];
lab.frame=CGRectMake(5, 0,320,20);
lab.text=[[Singleton sharedInstance] getTitleSection:week inDay:day];
lab.backgroundColor=[UIColor clearColor];
lab.textColor=[UIColor whiteColor];
lab.font = [UIFont fontWithName:#"Arial" size:14];
UIImageView * imv = [[[UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 20)]autorelease];
imv.image=[UIImage imageNamed:#"section-header-bg.png"];
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)]autorelease];
[view addSubview:imv];
[view addSubview:lab];
Week *currentWeek = nil;
if(week)
currentWeek = nechetNedel;
else
currentWeek = chetNedel;
NSMutableArray *dayArray = [currentWeek.days objectAtIndex:day];
if([dayArray count] >0)
return view;
return nil;
}
What can be the problem, why does lines of sections appear in the 5.0 simulator? I tried to delete the method - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section but it did not help. I delete this method(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section and the lines disappear
You should return nil for empty sections
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
and
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
should return 0
Then it will work. It is iOS 5.x issue (or feature)
I am trying to generate a UITextField in a view like this. I don't mind using IB or doing it programmatically.
(source: echofon.com)
New File(Cocoa Touch Class -> UIViewController subclass)
UITableViewController subclass, With XIB for interface(check both)
open Xib file
change the view style(plain->group) on Attributes Inspector(apple key + 1)
add following code in .m file
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (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.selectionStyle = UITableViewCellSelectionStyleNone;
}
if(indexPath.row == 0) {
cell.textLabel.text = #"Username";
UITextField *field = [[[UITextField alloc] initWithFrame: CGRectMake(120, 10, 180, 30)] autorelease];
[cell addSubview:field];
}
else if(indexPath.row == 1) {
cell.textLabel.text = #"Password";
UITextField *field = [[[UITextField alloc] initWithFrame: CGRectMake(120, 10, 180, 30)] autorelease];
field.secureTextEntry = YES;
[cell addSubview:field];
}
return cell;
}
- (NSString *)tableView: (UITableView *)tableView
titleForHeaderInSection: (NSInteger)section
{
return #"Account";
}
- (NSString *)tableView: (UITableView *)tableView
titleForFooterInSection: (NSInteger)section
{
return #"If you don't have a twitter account, go to twitter.com to sign up.";
}
You need to place the UITextField objects inside of a table to achieve this effect. See UITableView for more details: http://developer.apple.com/iphone/library/DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html