I have a table view with custom cells ,there are uibuttons on custom cell ,if i select button except that cell remaining all cells should be grayouted or disabled is it possible.
// code in tableview class
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
NSLog(#"No OF rows:%d",[contents count]);
return [contents count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellIdentifier = #"cell";
// Try to retrieve from the table view a now-unused cell with the given identifier.
cell = (uploadCustomCell *)[tableView dequeueReusableCellWithIdentifier:#"uploadCustomCell"];
if (cell == nil) {
NSLog(#"cell allocated");
// Use the default cell style.
cell = [[uploadCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"uploadCustomCell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"uploadCustomCell"
owner:self options:nil];
cell = [nib objectAtIndex:0];
}
saveBtnCcell.hidden = YES;
cell.textNamefield.hidden = YES;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.defaultSwitch setEnabled:NO];
dictionaryContents = [contents objectAtIndex:indexPath.row];
NSLog(#"dict dict :%#",dictionaryContents);
//
cell
.nameLabelCell.text = [dictionaryContents valueForKey:#"VideoName"];
cell.userName.text = [dictionaryContents valueForKey:#"User"];
NSLog(#"Array Image:%#",arrayimage);
cell.thumbImg.image = [arrayimage objectAtIndex:indexPath.row];
NSLog(#"ARimage:%#,index%d",[arrayimage objectAtIndex:indexPath.row],indexPath.row);
NSString *defaultVideo = [dictionaryContents valueForKey:#"DefaultVideo"];
NSLog(#"Default Video:%#",defaultVideo);
if ([defaultVideo isEqual: #"1"]) {
// [cell.defaultSwitch setOn:YES animated:YES];
[defaultSwitche setOn:YES animated:YES];
}
else{
// [cell.defaultSwitch setOn:NO animated:YES];
[defaultSwitche setOn:NO animated:YES];
}
[cell.defaultSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventValueChanged];
VideoNameTextField.hidden = YES;
return cell;
}
// Code in customcell
#interface uploadCustomCell (){
UploadAllViewController *uploadAll;
}
#end
#implementation uploadCustomCell
#synthesize textNamefield;
#synthesize savebtn,edit,nameLabelCell,textLabel,uploadBTN;
#synthesize defaultSwitch;
//#synthesize uploadAll;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[_userName release];
[_thumbImg release];
//[savebtn release];
[textNamefield release];
[nameLabelCell release];
[_test release];
[savebtn release];
[defaultSwitch release];
[uploadBTN release];
[super dealloc];
}
- (IBAction)editAction:(id)sender {
[uploadBTN setEnabled:NO];
uploadAll = [[UploadAllViewController alloc]init];
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:uploadAll.tabelView1];
NSIndexPath *indexPath = [uploadAll.tabelView1 indexPathForRowAtPoint:buttonPosition];
int no = indexPath.row;
NSLog(#"index path :%d",no);
[uploadAll didEditButtonPressed:self];
}
- (IBAction)saveBtnAction:(id)sender {
[uploadBTN setEnabled:YES];
[uploadAll didSaveButtonPressed:self];
}
when i select this editAction: except that cell remaining cells should be grayouted.
In your cellForRowAtIndexPath you have to account for the state of your table view, i.e. if one or zero cells are selected. Use that to change the appearance of your cell as you wish. In the example below I have assumed you are having a straight array without any sections, but the same principle would work with indexPaths as well. I use an int selectedRow set to -1 if there is no cell selected.
#define kNoCellSelected -1
// in cellForRowAtIndexPath:
if (self.selectedRow == kNoCellSelected) {
cell.backgroundView.backgroundColor = normalColor;
cell.userInteractionEnabled = YES;
}
else if (self.selectedRow != indexPath.row) {
cell.backgroundView.backgroundColor = disabledColor;
cell.userInteractionEnabled = NO;
}
Don't forget to set selectedRow in didSelectRowAtIndexPath: and in viewDidLoad.
Related
Am using below code for uitableview expand and collapse.Its works fine.But,when i select any section its expand but not collapse the previously expanded section.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
NSLog(#"expanded");
[expandedSections removeIndex:section];
screenTypeReloadCheck = NO;
}
else
{
screenTypeReloadCheck =YES;
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"UITableExpand"]];
cell.accessoryView = imView;
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"UITableContract"]];
cell.accessoryView = imView;
}
}
}
}
Thanks
I've done same thing in my app.. Here is solution ..
in your .h
#import "sampleCustomCell.h"
#interface second : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
NSMutableDictionary *selectedIndexes;
sampleCustomCell *samplCustom;
UITableView *tblTempData;
NSMutableArray *yourAry;
}
#property(nonatomic,retain) sampleCustomCell *samplCustom;
#property (nonatomic,retain) NSMutableArray *yourAry;
#property (nonatomic,retain) IBOutlet UITableView *tblTempData;
#end
in your .m
#synthesize samplCustom;
#synthesize tblTempData;
BOOL isSelected;
int kCellHieght=0;
- (void)viewDidLoad
{
[super viewDidLoad];
yourAry = [[NSMutableArray alloc] initWithObjects:#"1_id",#"2_id",#"3_id",#"4_id",#"5_id",#"6_id",#"7_id",#"8_id",#"9_id",#"10_id", nil];
selectedIndexes = [[NSMutableDictionary alloc] init];
}
#pragma mark TableView with Expand and collapse
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSLog(#"%#", selectedIndexes);
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
NSLog(#"%#", selectedIndex);
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self cellIsSelected:indexPath])
{
kCellHieght = 200; //Expanded height for your customCell xib.
}
else
{
kCellHieght = 130; // Height of your customCell xib
}
return kCellHieght;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [yourAry count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
sampleCustomCell *cell;
if (tableView.tag == 11)
{
cell = (sampleCustomCell *)[tableView dequeueReusableCellWithIdentifier:#"sampleCustomCell"];
NSArray *nib;
for (UIControl *subview in cell.contentView.subviews) {
[subview removeFromSuperview];
}
if(cell == nil)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"sampleCustomCell" owner:self options:nil];
for(id oneobject in nib)
{
if([oneobject isKindOfClass:[sampleCustomCell class]])
{
cell = (sampleCustomCell *)oneobject;
//[cell setIndexpath:indexPath];
//[cell setDelegete:self];
cell.lblProduct.text = [yourAry objectAtIndex:[indexPath row]];
}
}
}
if([self cellIsSelected:indexPath])
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3];
[UIView commitAnimations];
[self performSelector:#selector(showHidden:) withObject:cell afterDelay:0.4];
}
}
return cell;
}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
isSelected = ![self cellIsSelected:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
BOOL isSelected = ![self cellIsSelected:indexPath];
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes removeAllObjects];
[self hideTV:tableView];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
NSLog(#" array %#", selectedIndexes);
[self.tblTempData beginUpdates];
sampleCustomCell *selectedCell = (sampleCustomCell *)[tblTempData cellForRowAtIndexPath:indexPath];
if([self cellIsSelected:indexPath])
{
samplCustom = selectedCell;
[UIView animateWithDuration:15 delay:0.2 options:UIViewAnimationCurveLinear animations:^{NSLog(#"animating");} completion:^(BOOL finished){
NSLog(#"Done 1!");
}];
[UIView commitAnimations];
}
else
{
[UIView animateWithDuration:5 delay:0.2 options:UIViewAnimationCurveLinear animations:^{NSLog(#"animating");} completion:^(BOOL finished){
NSLog(#"Done 1!");
}];
[UIView commitAnimations];
}
[self.tblTempData endUpdates];
[tblTempData reloadData];
}
-(void)hideTV:(UIView *) view{
[tblTempData reloadData];
for (id View in [view subviews]) {
if ([View isKindOfClass:[UITextView class]]) {
[View setHidden:YES];
}
if ([View isKindOfClass:[UITableViewCell class]]) {
UITableViewCell *cell = (UITableViewCell *) View;
[self hideTV:cell.contentView];
}
}
}
-(void) showHidden : (UITableViewCell *)cell{
for (id View in [cell subviews]) {
if ([View isKindOfClass:[UITextView class]]) {
if ([View tag] == 111) {
[View setHidden:NO];
}
}
}
}
Hopr this helps.. happy coding.. Thanks..
TableView section is not collapse but expand. Because, Your currentlyExpanded is always return NO. SO, you are always getting expand.
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
NSLog(#"expanded");
[expandedSections removeIndex:section];
screenTypeReloadCheck = NO;
}
else
{
screenTypeReloadCheck =YES;
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
I do not know how do you set currentlyExpanded value. If you need more help, please share few more lines of code.
RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
NSMutableArray *petsArray;
}
#property (nonatomic, strong) NSMutableArray *petsArray;
#end
RootViewController.m
#import "RootViewController.h"
#import "PetsViewController.h"
#interface RootViewController ()
#end
#implementation RootViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
petsArray = [[NSMutableArray alloc] init];
[petsArray addObject:#"Dog"];
[petsArray addObject:#"Cat"];
[petsArray addObject:#"Snake"];
[self setTitle:#"PETS !"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [petsArray 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];
}
// Configure the cell...
cell.textLabel.text = [petsArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
I think that the problem is in didSelectRowAtIndexPath which I can't use in storyboarded project.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PetsViewController *pets = [[PetsViewController alloc] initWithNibName:#"PetsViewController" bundle:nil];
if ([[petsArray objectAtIndex:indexPath.row] isEqual:#"Dog"]) {
pets.petsInt = 0;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
if ([[petsArray objectAtIndex:indexPath.row] isEqual:#"Cat"]) {
pets.petsInt = 1;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
if ([[petsArray objectAtIndex:indexPath.row] isEqual:#"Snake"]) {
pets.petsInt = 2;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
[self.navigationController pushViewController:pets animated:YES];
}
#end
PetsViewController.h
#import <UIKit/UIKit.h>
#interface PetsViewController : UITableViewController {
NSMutableArray *dogArray;
NSMutableArray *catArray;
NSMutableArray *snakeArray;
int petsInt;
}
#property int petsInt;
#end
PetsViewController.m
#import "PetsViewController.h"
#interface PetsViewController ()
#end
#implementation PetsViewController
#synthesize petsInt;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dogArray = [[NSMutableArray alloc] initWithObjects:#"HAF",#"hafo", #"hafinio", nil];
catArray = [[NSMutableArray alloc] initWithObjects:#"MYAU",#"myainio", #"mya lya lya", nil];
snakeArray = [[NSMutableArray alloc] initWithObjects:#"fshhhhh",#"fsssss", #"xrt", nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (petsInt == 0) return [dogArray count];
if (petsInt == 1) return [catArray count];
if (petsInt == 2) return [snakeArray count];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PetsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (petsInt == 0) cell.textLabel.text = [dogArray objectAtIndex:indexPath.row];
if (petsInt == 1) cell.textLabel.text = [catArray objectAtIndex:indexPath.row];
if (petsInt == 2) cell.textLabel.text = [snakeArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
[self performSegueWithIdentifier:#"SEGUE_NAME" sender:self];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"SEGUE_NAME"]) {
PetsViewController *pets = [segue destinationViewController];
[pets setPetsInt:0];
[pets setTitle:#"Title"];
}
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
if ([[gazel objectAtIndex:indexPath.row] isEqual:#"<էրեբունի> Օդանավակայան"]) {
KangarList *kang = [segue destinationViewController];
kang.kangarInt = 0;
[kang setTitle:[gazel objectAtIndex:indexPath.row]];
}
if ([[gazel objectAtIndex:indexPath.row] isEqual:#"<էրեբունի> Օդանավակայան"]) {
KangarList *kang = [segue destinationViewController];
kang.kangarInt = 1;
[kang setTitle:[gazel objectAtIndex:indexPath.row]];
}
if ([[gazel objectAtIndex:indexPath.row] isEqual:#"<էրեբունի> Օդանավակայան"]) {
KangarList *kang = [segue destinationViewController];
kang.kangarInt = 2;
[kang setTitle:[gazel objectAtIndex:indexPath.row]];
}
if ([[gazel objectAtIndex:indexPath.row] isEqual:#"<էրեբունի> Օդանավակայան"]) {
KangarList *kang = [segue destinationViewController];
kang.kangarInt = 3;
[kang setTitle:[gazel objectAtIndex:indexPath.row]];
}
}
i have tried many ways and i finaly realized the right method, but it works only when under view did load method objects have only one description but if it hase something like thise
gazel = [[NSMutableArray alloc] init];
Data *data = [[Data alloc] init];
data.title = #"<էրեբունի> Օդանավակայան";
data.subtitle = #"Հարաֆ Արևմտյան Թաղամաս";
data.photo = 1;
[gazel addObject:data];
its not working
I am making an application in which I have to read the contacts from an ABAddressBook.
I have read the data and stored it in a dictionary.I want to display the names
alphabetically in every section. Each object in the dictionary refers to the details of a single person. Any help ?
(
{
letter = D;
name = zzzz;
telephone = "1234566";
},
{
letter = R;
name = "ffff";
telephone = "332333";
},
{
letter = A;
name = aaaaa;
telephone = "1112226";
},
{
letter = s;
name = ssssss;
telephone = "234 56792";
},
{
letter = K;
name = "Klll";
telephone = "(888) 888-8888";
}
)
use the following link,
http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007318
I am sending code, I have done already this type of task. Actually your problem you not able sort dictictionary.
please refer my code for reference.
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import "AddressBookUI/AddressBookUI.h"
#interface AddressBookViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,ABNewPersonViewControllerDelegate>
{
IBOutlet UITableView* addressNameView;
ABAddressBookRef _addressBook;
}
#property(nonatomic,retain)UITableView* addressNameView;
#property (nonatomic, retain) NSMutableArray *contacts;
#property (nonatomic, retain)IBOutlet UILabel *titleText;
#property(nonatomic,retain)IBOutlet UISegmentedControl *segmentControl;
-(IBAction)backAction:(id)sender;
#end
/****************/
#import "AddressBookViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AddressEntryDetailController.h"
#import "MenuViewController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
NSInteger static compareViewsByOrigin(id sp1, id sp2, void *context)
{
// UISegmentedControl segments use UISegment objects (private API). Then we can safely
// cast them to UIView objects.
float v1 = ((UIView *)sp1).frame.origin.x;
float v2 = ((UIView *)sp2).frame.origin.x;
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
#implementation AddressBookViewController
#synthesize addressNameView,contacts;
#synthesize titleText,segmentControl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didChangeSegmentControl:(UISegmentedControl *)control
{
int index=control.selectedSegmentIndex;
NSLog(#"Slected Index : %d",index);
if(index==1)
{
[self getAllContacts];
}
else
{
[self getAllContactsByRelevence];
}
}
-(void)getAllContacts
{
ABRecordRef source = ABAddressBookCopyDefaultSource(_addressBook);
NSArray *tempArray = (NSArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(_addressBook, source, kABPersonSortByFirstName));
[contacts removeAllObjects];
if ([tempArray count] > 0)
{
[contacts addObjectsFromArray:tempArray];
}
[addressNameView reloadData];
titleText.text=#"All";
}
-(void)getAllContactsByRelevence
{
ABRecordRef source = ABAddressBookCopyDefaultSource(_addressBook);
NSArray *tempArray = (NSArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(_addressBook, source, kABPersonSortByLastName));
[contacts removeAllObjects];
if ([tempArray count] > 0)
{
[contacts addObjectsFromArray:tempArray];
}
[addressNameView reloadData];
titleText.text=#"All";
}
- (void)viewDidLoad
{
_addressBook = ABAddressBookCreate();
contacts=[[NSMutableArray alloc]init];
[self getAllContacts];
addressNameView.layer.borderWidth=2;
addressNameView.layer.cornerRadius=13;
addressNameView.layer.borderColor =[UIColor colorWithRed:192.0f/255.0f green:189.0f/255.0f blue:189.0f/255.0f alpha:1.0f].CGColor;
addressNameView.backgroundColor=[UIColor colorWithRed:236.0f/255.0f green:236.0f/255.0f blue:235.0f/255.0f alpha:1.0f];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self CreateAlphabetButton];
self.segmentControl.selectedSegmentIndex=1;
[self.segmentControl addTarget:self action:#selector(didChangeSegmentControl:) forControlEvents:UIControlEventValueChanged];
}
-(void)CreateAlphabetButton
{
int yPoint=110;
NSString *list=#"*ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int y=1;y<=27;y++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(720,yPoint,50,28);
yPoint+=29;
button.titleLabel.text=[list substringWithRange:NSMakeRange(y-1, 1)];
UILabel *readding=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,40, 30)];
readding.text=[list substringWithRange:NSMakeRange(y-1, 1)];
readding.textColor=[UIColor darkTextColor];
[readding setTextAlignment:UITextAlignmentCenter];
readding.font=[UIFont boldSystemFontOfSize:20.0];
readding.backgroundColor = [UIColor clearColor];
[button addSubview:readding];
[button addTarget:self action:#selector(toggleOpen:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
}
}
-(IBAction)toggleOpen:(id)sender
{
UIButton *btn=(UIButton*)sender;
NSLog(#"%#",btn.titleLabel.text);
//for searching
if([btn.titleLabel.text isEqualToString:#"*"])
{
[self getAllContacts];
titleText.text=#"All";
}
else
{
NSArray *tempArray = (NSArray *)(ABAddressBookCopyPeopleWithName(_addressBook,(CFStringRef)btn.titleLabel.text));
[contacts removeAllObjects];
if ([tempArray count] > 0)
{
[contacts addObjectsFromArray:tempArray];
}
[addressNameView reloadData];
titleText.text=btn.titleLabel.text;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(IBAction)backAction:(id)sender
{
//MenuViewController *menuView=[[MenuViewController alloc]initWithNibName:#"MenuViewController" bundle:[NSBundle mainBundle]];
//[self presentModalViewController:menuView animated:YES];
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([self.contacts count]==0)
return 1;
return [self.contacts count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableViewCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.tag = indexPath.row;
if([self.contacts count]>0)
{
NSObject *object = [self.contacts objectAtIndex:indexPath.row];
NSString *firstName=[(NSString *)ABRecordCopyValue((ABRecordRef)object, kABPersonFirstNameProperty) autorelease];
NSString *lastName =[(NSString *)ABRecordCopyValue((ABRecordRef)object, kABPersonLastNameProperty) autorelease];
if(lastName)
cell.textLabel.text=[NSString stringWithFormat:#"%# %#",firstName,lastName];
else
cell.textLabel.text=firstName;
}
else
{
cell.textLabel.text=#"No Contacts";
}
//UISwipeGestureRecognizer* gestureR;
//gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)] autorelease];
// gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
// [cell addGestureRecognizer:gestureR];
// cell.editing=YES;
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.contacts count]>0)
return UITableViewCellEditingStyleDelete;
else
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSLog(#"Delete Clicked on cell Index : %d",indexPath.row);
// Commit the delete
ABRecordRef* personObject=(ABRecordRef*) [self.contacts objectAtIndex:indexPath.row];
if(ABAddressBookRemoveRecord(_addressBook, personObject, NULL))
{
ABAddressBookSave(_addressBook, NULL);
[self getAllContacts];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Contact succesfully deleted"
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
/*
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"%d = %d",recognizer.direction,recognizer.state);
}
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 54;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.contacts count]>0)
{
AddressEntryDetailController *entryDetail=[[AddressEntryDetailController alloc]initWithNibName:#"AddressEntryDetailController" bundle:[NSBundle mainBundle]];
int index=indexPath.row;
entryDetail.personObject=(ABRecordRef*) [self.contacts objectAtIndex:index];
entryDetail._addressBook=&_addressBook;
entryDetail.addressBookViewController=self;
[self presentModalViewController:entryDetail animated:YES];
}
}
-(IBAction)addToAddressbook:(id)sender
{
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
[self presentModalViewController:navigation animated:YES];
[picker release];
[navigation release];
/* ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];*/
}
#pragma mark ABNewPersonViewControllerDelegate methods
// Dismisses the new-person view controller.
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
[self dismissModalViewControllerAnimated:YES];
[self getAllContacts];
}
Right Now I am making a twitter app and I am Using IFTweetLabel. I made a TableViewCell Subclass but When I Run it the label dosent show up.
Here is the .h:
#import <UIKit/UIKit.h>
#import "IFTweetLabel.h"
#interface twitterTextCell : UITableViewCell
#property (strong, nonatomic) IFTweetLabel *customtextLabel;
#end
And here is the implementation:
#import "twitterTextCell.h"
#implementation twitterTextCell
#synthesize customtextLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
customtextLabel.backgroundColor = [UIColor orangeColor];
self.customtextLabel.frame = CGRectMake(240, 60, 345, 109);
[self addSubview:customtextLabel];
NSLog(#"Custom Label Text: %#", customtextLabel.text);
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(UILabel *)textLabel
{
return nil;
}
Here is the code that is in the cellforrow in the tableviewdelegate:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
twitterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[twitterTextCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tweet = [self.timeLineData objectAtIndex:indexPath.row];
if ([tweet objectForKey:#"text"] != nil) {
NSString *allText = [[tweet objectForKey:#"text"] stringByDecodingHTMLEntities];
cell.customtextLabel.numberOfLines = 4;
cell.textLabel.numberOfLines = 4;
cell.customtextLabel.text = allText;
cell.textLabel.text = allText;
cell.customtextLabel.linksEnabled = YES;
NSDictionary *whoPostedTweet = [tweet objectForKey:#"user"];
cell.detailTextLabel.text = [whoPostedTweet objectForKey:#"name"];
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#", [whoPostedTweet objectForKey:#"profile_image_url"]]];
[cell.imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.backgroundColor = [UIColor clearColor];
[cell.imageView.layer setCornerRadius:7.0f];
[cell.imageView.layer setMasksToBounds:YES];
return cell;
} else {
[self getTwitterData];
NSLog(#"nil called(else) ");
return cell;
}
return cell;
}
After all of this only the image and the detailtextview show up. I removing the default textview because If I don't the text shows up normally but it is not the IFTweetLabel. Another thing I have a question about is how do I reposition the detailtextabel so that it is Under the IFTweetLabel. Anything is greatly appreciated, I need help on both of these issues.
i think your problem is you are not allocating customTextLabel.
Just allocate the same before setting the background color in initWithStyle.
ie, self.customtextLabel = [[[IFTweetLabel alloc] init] autorelease];
I have a storyboard with a UIView and a UITableView in it. The UITableView has custom cells (UITableViewCell with xib).
When I run the app, only the first row of the table is shown, the rest of the table is hidden.
The rows are not seen on the screen, but they respond to user interaction as expected.
The rest of the cells are revealed only after the table is scrolled upwards.
What could be the problem?
Here is the code for UIView with the table:
#implementation EldersTableViewController
#synthesize items;
#synthesize tableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
EldersDataHendler *edh = [[EldersDataHendler alloc]init];
NSMutableArray *itemsFromPList = [edh dataForTableFrom:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"eldersData" ofType:#"plist"]]];
self.items = itemsFromPList;
[edh release];
edh=nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (IBAction)didPressBackButton:(UIButton *)sender {
[self.view removeFromSuperview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SEObject *cellInfo = [self.items objectAtIndex:indexPath.row];
EldersItemCell* cell = (EldersItemCell*)[tableView dequeueReusableCellWithIdentifier:#"EldersItemCell"];
if (cell == nil) {
NSArray* topObjects = [[NSBundle mainBundle] loadNibNamed:#"EldersItemCell" owner:self options:nil];
for (id obj in topObjects){
if ([obj isKindOfClass:[EldersItemCell class]]){
cell = (EldersItemCell*)obj;
break;
}
}
}
[cell setObject:cellInfo];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SEObject *cellInfo = [self.items objectAtIndex:indexPath.row];
if (!eldersDetailsViewController){
eldersDetailsViewController = [[self.storyboard instantiateViewControllerWithIdentifier:#"elderDetailsVC"] retain];
}
eldersDetailsViewController.seObject = cellInfo;
[self.view addSubview:eldersDetailsViewController.view];
}
- (void)dealloc {
[eldersDetailsViewController release];
[tableView release];
[super dealloc];
}
#end
Thanks in advance,
Luda
1st just create an object class for your cell design,then import it in your .m file.then try like this and connect the table view delegate and data source properly
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Displayfriendscell";
Displayfriendscell *cell = (Displayfriendscell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[Displayfriendscell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell...
[[cell viewWithTag:121] removeFromSuperview];
[[cell viewWithTag:151] removeFromSuperview];
friend *user = [sortedFriendsArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.nameLabel.text = user.contactNAame;
cell.nameLabel.text = user.friendName;
//cell.nameLabel.text =[[sortedFriendsArray objectAtIndex:indexPath.row] objectForKey:#"name"];
//user.friendName=[[sortedFriendsArray objectAtIndex:indexPath.row] objectForKey:#"name"];
NSLog(#"name is%#",cell.nameLabel.text);
return cell;
}
Problem solved! Thanks a lot dear arooaroo. There was something with the custom cell with the xib. So I've created a custom Table View Cell programmatically. Here is a nice tutorial