Reorder tableview cell issues - ios

I just created an assessment in Core Data. My process is to reorder them using button inside tableview.
I took my dynamic button inside tableview but I cant reorder them in tableview cell please help me out. Its like drag and drop.
table=[[UITableView alloc]init];
table.frame=CGRectMake(prev.frame.size.width*1.2, questionlabel.frame.size.height*1.5,self.view.frame.size.width/1.6, self.view.frame.size.height/2);
table.backgroundColor=[UIColor blueColor];
[table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.view addSubview:table];
-(void)addbutton:(NSString *)desc bid:(int)bid{
[[QBFlatButton appearance] setFaceColor:[UIColor colorWithWhite:0.75 alpha:1.0] forState:UIControlStateNormal];
[[QBFlatButton appearance] setSideColor:[UIColor colorWithWhite:0.55 alpha:1.0] forState:UIControlStateNormal];
optionButton = [QBFlatButton buttonWithType:UIButtonTypeCustom];
optionButton.faceColor = [UIColor colorWithRed:86.0/255.0 green:161.0/255.0 blue:217.0/255.0 alpha:1.0];
optionButton.sideColor = [UIColor colorWithRed:79.0/255.0 green:127.0/255.0 blue:179.0/255.0 alpha:1.0];
optionButton.radius = 4.0;
optionButton.margin = 4.0;
optionButton.depth = 3.0;
[optionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
optionButton.titleLabel.font=[UIFont systemFontOfSize:12];
optionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// you probably want to center it
optionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
// [optionButton addTarget:self
// action:#selector(dynamicclick:)
// forControlEvents:UIControlEventTouchUpInside];
optionButton.tag=bid;
[optionButton setTitle:desc forState:UIControlStateNormal];
optionButton.frame = CGRectMake(5,bid*55,table.frame.size.width-10, 50);
[table addSubview:optionButton];
}
- (void)initTableItem {
self.items = [NSMutableArray array];
[self.items addObject:optionButton];
}
- (void)logItems {
int index = 0;
for ( NSString *str in self.items ) {
NSLog(#"%d: %#", index++, str);
}
}
#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.layoutMargins=UIEdgeInsetsZero;
[cell addSubview:optionButton];
return cell;
}
#pragma mark - Edit Mode
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone; // No Delete icon
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
[table deselectRowAtIndexPath:indexPath animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Can move cell
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSUInteger origins = sourceIndexPath.row; // Original position
NSUInteger to = destinationIndexPath.row; // Destination position
NSLog(#"Origin %lu, To %lu", (unsigned long)origins, (unsigned long)to);
NSString *swap = [self.items objectAtIndex:origin];// Item
[swap shouldGroupAccessibilityChildren];
}

Related

Getting buttons inside tableview

I want to add button inside UITableView with drag and drop but i don't know how to call them inside UITableviewCell.You can see a keyword Desc where I have to set them as button title so that I can get all values which I placed in coreData frameWork.
-(void)setquestions:(int)qid
{
NSManagedObject *singleobject = nil;
singleobject = AllQuestions[qid];
NSString * desc=[singleobject valueForKey:#"questionDesc"];
questionlabel.text=desc;
int qidns=[[singleobject valueForKey:#"questionId"]intValue];
NSArray *dummy=[self getoptions:qidns];
NSLog(#"--inside setquestion-%lu",(unsigned long)dummy.count);
for (int i=0; i<dummy.count; i++) {
NSManagedObject *singleobject = nil;
singleobject = dummy[i];
NSString * desc=[singleobject valueForKey:#"optionDesc"];
[self addbutton:desc bid:i];
}
}
-(void)addbutton:(NSString *)desc bid:(int)bid{
[[QBFlatButton appearance] setFaceColor:[UIColor colorWithWhite:0.75 alpha:1.0] forState:UIControlStateNormal];
[[QBFlatButton appearance] setSideColor:[UIColor colorWithWhite:0.55 alpha:1.0] forState:UIControlStateNormal];
optionButton = [QBFlatButton buttonWithType:UIButtonTypeCustom];
optionButton.faceColor = [UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f];
optionButton.sideColor = [UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f];
optionButton.radius = 4.0;
optionButton.margin = 4.0;
optionButton.depth = 3.0;
[optionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
optionButton.titleLabel.font=[UIFont systemFontOfSize:12];
optionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// you probably want to center it
optionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
optionButton.tag=bid;
[optionButton setTitle:desc forState:UIControlStateNormal];
optionButton.frame = CGRectMake(5,bid*55,optionsview.frame.size.width-10, 50);
[optionsview addSubview:optionButton];
}
- (void)logItems {
int index = 0;
for ( NSString *str in self.items ) {
NSLog(#"%d: %#", index++, str);
}
}
#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
return cell;
}
#pragma mark - Edit Mode
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone; // No Delete icon
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
[table deselectRowAtIndexPath:indexPath animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Can move cell
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSUInteger origins = sourceIndexPath.row; // Original position
NSUInteger to = destinationIndexPath.row; // Destination position
NSLog(#"Origin %lu, To %lu", (unsigned long)origins, (unsigned long)to);
NSString *swap = [self.items objectAtIndex:origin];// Item
[swap shouldGroupAccessibilityChildren];
}
As I understood from the question you want to get the button in the cell for that you can create a custom cell class and then call this custom cell inside the cellforrow function. You can call the button like this
customCell.button.title = "some-string"
If you want a custom button with a particular action added to each of the cells, first you need to create a custom UI cell(using XIB since you need to drag and drop). Then add the button to that, and connect the button's IBAction and IBOutlet to the custom subclass for the cell. Next Add a protocol with a function to the cell subclass so that you can send a callback to the ViewController when the button is clicked. Now implement the delegate in the ViewController and use this cell for your purpose. If the delegate function returns the cell, you can figure out which cell's button is clicked, hence getting the indexpath to perform required action.

UITableView unresponsive to gestures and not populating cells with data

I created a UITableView programmatically and set all of its attributes, but the data from my NSMutableArray will not populate the cells and the cells do nothing when tapped. Any insight appreciated, thanks!
Clayton
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
list = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * .3333, self.view.bounds.size.width, self.view.bounds.size.height * .6667) style:UITableViewStylePlain];
list.delegate = self;
list.dataSource = self;
list.backgroundColor = [UIColor magentaColor];
list.scrollEnabled = YES;
list.userInteractionEnabled = YES;
list.multipleTouchEnabled = YES;
list.hidden = NO;
[self.view addSubview:list];
[self.view bringSubviewToFront:list];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [favorites count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cell = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cell];
}
cell.textLabel.text = [[global sharedInstance].favorites objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cell.userInteractionEnabled = YES;
cell.hidden = NO;
cell.multipleTouchEnabled = YES;
[self.view bringSubviewToFront:cell];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"selected %ld row", (long)indexPath.row);
}
Got it thanks, the problem was with the data transferring from my singleton array of favorites to my local array (which was previously empty). Much obliged :)
At the end of viewDidLoad method, just add this:
[list reloadData];

create an action of UI label in tableviewcell

I am making an inbox module in my app.On first load 20 messages are coming from server.
I want that after 20 messages a label named "load more messages" will be create in UItableview cell.
and after clicking on that label i want to make another call to server.
I had tried following code but it is not working . thanx in advance:(
below is my sample code of cellForRowAtIndexPath and numberOfRowsInSection
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [inboxmessagesarray count]+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableviewidentifier = #"cell";
__block tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell==nil)
{
cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}if(indexPath.row == [self tableView:self.activitiesTableView_ numberOfRowsInSection:indexPath.section] - 1){
cell.textLabel.text=#"Load More Record";// here i am making a label but it is not showing at the end of tableview cell
}
else{
__block NSString *row = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
cell.titlename.font=[UIFont fontWithName:#"SegoeUI" size:15];
cell.tolbl.font=[UIFont fontWithName:#"SegoeUI-light" size:12];
cell.fromlbl.font=[UIFont fontWithName:#"SegoeUI-light" size:12];
cell.datelbl.font=[UIFont fontWithName:#"SegoeUI-light" size:8];
cell.timelbl.font=[UIFont fontWithName:#"SegoeUI-light" size:8];
if([[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:#"messageRead"] intValue]==0)
{
cell.titlename.font=[UIFont fontWithName:#"SegoeUI" size:15];
cell.tolbl.font=[UIFont fontWithName:#"SegoeUI-light" size:12];
cell.fromlbl.font=[UIFont fontWithName:#"SegoeUI-light" size:12];
cell.datelbl.font=[UIFont fontWithName:#"SegoeUI-light" size:8];
cell.timelbl.font=[UIFont fontWithName:#"SegoeUI-light" size:8];
cell.contentView.backgroundColor=[UIColor lightGrayColor];
}
else
{
cell.titlename.font=[UIFont fontWithName:#"SegoeUI" size:15];
cell.tolbl.font=[UIFont fontWithName:#"SegoeUI-light" size:12];
cell.fromlbl.font=[UIFont fontWithName:#"SegoeUI-light" size:12];
cell.datelbl.font=[UIFont fontWithName:#"SegoeUI-light" size:8];
cell.timelbl.font=[UIFont fontWithName:#"SegoeUI-light" size:8];
cell.contentView.backgroundColor=[UIColor clearColor];
}
cell.titlename.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:#"offerTitle"];
//toCity
cell.tolbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:#"toCity"];
cell.fromlbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:#"fromCity"];
if(![[imagesDictionary allKeys] containsObject:row]) //if image not found download and add it to dictionary
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSString *img=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:#"offerPhoto"];// here i am getting image path
NSURL *url = [NSURL URLWithString:img];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^{ //in main thread update the image
[imagesDictionary setObject:image forKey:row];
cell.profileimage.image = image;
cell.textLabel.text = #""; //add this update will reflect the changes
NSLog(#"loading and adding to dictionary");
});
});
}
else
{
cell.profileimage.image = [imagesDictionary objectForKey:row];
NSLog(#"retriving from dictionary");
}
cell.datelbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:#"messageDate"];
cell.timelbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:#"messageTime"];
}
return cell;
}
As u want a row to be added to tableview that shows Load More Records u can do it in 2 ways either by adding a footer view or every last row, by passing one extra row in the method -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and return a particular cell for this row in -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for example add one more custom cell in the tableview and add a label and set its text as Load more record and set its reuse identifier as LabelCell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.inboxmessagesarray.count + 1 ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//[self performSegueWithIdentifier:#"segueIdentifier" sender:tableView];
if(indexPath.row == [self.inboxmessagesarray count])
{
NSLog(#"load more records");
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
__block tablecellTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell == nil)
{
cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewidentifier];
}
//in the last row
if(indexPath.row == self.inboxmessagesarray.count)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"LabelCell"];
if(cell == nil)
{
cell = [[tablecellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"LabelCell"];
}
if([self.inboxmessagesarray count] > 0)
cell.hidden = NO;
else
cell.hidden = YES;
return cell;
}
//..rest of the code
try to use following Methods of UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView * viewHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
[viewHeader setBackgroundColor:[UIColor redColor]];
UIButton * btnLoadMore = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLoadMore setFrame:CGRectMake(10, 5, 300, 30)];
[btnLoadMore setTitle:#"Load More Record" forState:UIControlStateNormal];
[btnLoadMore setTintColor:[UIColor blackColor]];
[btnLoadMore setBackgroundColor:[UIColor clearColor]];
[btnLoadMore addTarget:self action:#selector(loadMoreRecords:) forControlEvents:UIControlEventTouchUpInside];
[viewHeader addSubview:btnLoadMore];
return viewHeader;
}
- (void)loadMoreRecords : (id) sender {
NSLog(#"Your code to load more data");
}
you can use this in multiple types here I add in 3 types, customize your self
choice No 1
assume that u get the all data (e.g 100) from server , store in the some NSMutableArray, concept is declare one global int
int loadMoreItems;
in your viewWillAppear
-(void)viewWillAppear:(BOOL)animated
{
loadMoreItems=21; // initially set the count is more than 20
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([yourTotalArrayName count]<=loadMoreItems) {
return [yourTotalArrayName count];
}
else {
return loadMoreItems;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
// add your data
if (indexPath.row+1<loadMoreItems)
{
cell.lblLoadMoreItems.hidden=YES;
cell.itemsdetails.text = [NSString stringWithFormat:#"%#",[yourTotalArrayName objectAtIndex:indexPath.row]] ;
}
else
{
cell.itemsdetails.hidden=YES;
cell.lblLoadMoreItems.text = #"Load more results...!";
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row+1<loadMoreItems) {
// here do your stuff here
}
else {
loadMoreItems=loadMoreItems+20;
[self.yourtableview reloadData];
}
choice no 2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Classic start method
// here add your data loaded
if (indexPath.row == [self.yourarrayName count] - 1)
{
cell.lblLoadMoreItems.text = #"Load more results...!";
[self launchReload];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [self.yourarrayName count] - 1)
{
[self launchReload];
}
}
-(void)launchRelaod
{
// call the webservice
[self.yourtableview reloadData];
}
choiceNo 3
use 2 prototype cell in `UItableView`
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [yourarrayname count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *postCellId = #"postCell";
static NSString *moreCellId = #"moreCell";
UITableViewCell *cell = nil;
if ([indexPath row] == [yourarrayname count]) {
cell = [tableView dequeueReusableCellWithIdentifier:moreCellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moreCellId];
}
cell.textLabel.text = #"Load more items...";
cell.textLabel.textColor = [UIColor blueColor];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:postCellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:postCellId];
}
// show your Data
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] == [yourarrayname count]) {
// call the web service and reload Data of UItableview
}
else
{
//normall selcftion procedure
}
}
Add your label as a Table's footerview.
tableview.tableFooterView = label;
By this you will get your label at the end of your table always.
And for load more action add tap gestures to the label.
If you just want something to show up at the bottom or below your tableView you just implement a quick step:
This will waive any auto layout woes or troubles with a UILabels frame or origins (it will never move)
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 25)];
UIButton *loadMoreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
[loadMoreButton setTitle:#"Load More" forState:UIControlStateNormal];
[loadMoreButton addTarget:self action:#selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
//More customizing
[footerView addSubview:loadMoreButton];
self.tableView.tableFooterView = footerView;
-(IBAction)loadMoreButton:(id)sender {
//load more from server
}

Nested UITableViewController does not show any cells

I've added a UITableViewController to my main view controller using method from Apple docs:
- (void)viewWillAppear:(BOOL)animated {
myTableViewController = [MyTableViewController new];
[self displayContentController:myTableViewController];
}
- (void)displayContentController:(UIViewController *)viewController; {
[self addChildViewController:viewController];
viewController.view.frame = self.view.bounds;
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
and I wanted to display some example cells in this controller:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
(I don't think I need any more configuration as it is UITableViewController, not UITableView)
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text = #"Under development";
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
But all I can see is just empty cells (I've also tried to change background color to make sure it's not just the while label color):
After few hours I have no idea what I did wrong. Do you have any idea what am I missing?
You have to used it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell=[[UITableViewCell alloc]init];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.text = #"Under development";
return cell;
}
Try it:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return Table_Arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"subwaysurfer"];
if(!cell)
{
cell=[[UITableViewCell alloc]init];
}
UIImageView *imgg;
UILabel *txt;
UIImageView *Acc_img;
imgg=[[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 50)];
imgg.image = [UIImage imageNamed:[imageArr objectAtIndex:indexPath.row]];
cell.highlighted=YES;
cell.backgroundColor=[UIColor clearColor];
txt=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-80, 60)];
txt.numberOfLines = 3;
txt.text=[Table_Arr objectAtIndex:indexPath.row];
txt.backgroundColor=[UIColor clearColor];
// txt.textColor=[UIColor colorWithRed:66.0/255.0 green:51.0/255.0 blue:57.0/255.0 alpha:1.0];
txt.textColor=[UIColor colorWithRed:44.f/255.f green:78.f/255.f blue:134.f/255.f alpha:1];
txt.font=[UIFont fontWithName:#"AvenirNextCondensed-Medium" size:16.0];
Acc_img=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, 20, 20, 20)];
Acc_img.image=[UIImage imageNamed:#"arrow.png"];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
imgg.frame = CGRectMake(10, 5, 80, 80);
txt.frame = CGRectMake(120, 10, self.view.frame.size.width-160, 70);
Acc_img.frame = CGRectMake(self.view.frame.size.width-80, 25, 40, 40);
txt.font=[UIFont systemFontOfSize:28.0];
}
[cell addSubview:imgg];
[cell addSubview:txt];
[cell.contentView addSubview:Acc_img];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
// fixed font style. use custom view (UILabel) if you want something different
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
return 90;
}
else{
return 60;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
}
Ok, problem solved. It appears, that even using UITableViewController which has built-in property tableView it is necessary to initialise the tableView (or at least it did help me).
self.tableView = [UITableView new];

UITableView with multiple sections and accessoryType

This is my functions for my table View.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.categories count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"test";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"%d", self.collections.count);
return [self.collections count];
}
- (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];
}
switch ([indexPath section])
{
case 0:
self.myList = self.collections;
break;
}
cell.textLabel.text = self.collections[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 48;
}
Actually, with this code, my table view show all the section and cells in the same view.
However, I would like a table view which show, at first time, a row with my title of section.
When i click on the row where there is my title of section, i would like to show the cells which are in the section. How can i do that?
Do i need 2 tableViews?
You should have to take two uitableview in that delegate will be same after that you should have to use this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%#",indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==Nil)
{
cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
if (isDeptSelected)
{
//[cell.imageView setImage:[UIImage imageNamed:#"Department.png"]];
cell.textLabel.text = [[arrDept_Detail valueForKey:#"dep_Name"]objectAtIndex:indexPath.row];
}
else if (isEmpSelected)
{
[cell.imageView setImage:[UIImage imageNamed:#"Emp_Images"]];
cell.textLabel.text = [[arrEmp_Detail valueForKey:#"emp_Name"]objectAtIndex:indexPath.row];
}
// cell.textLabel.text=[[arrDept_Detail objectAtIndex:indexPath.row]valueForKeyPath:#"dep_Name"];
[cell setBackgroundColor:[UIColor brownColor]];
[cell.textLabel setTextColor:[UIColor greenColor]];
[cell.textLabel setFont:[UIFont fontWithName:#"Georgia-Bold" size:16]];
cell.textLabel.highlightedTextColor=[UIColor purpleColor];
[self.view setBackgroundColor:[UIColor brownColor]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (isDeptSelected)
{
isDeptSelected=false;
isEmpSelected=true;
[btnDoneclick setHidden:NO];
[self.view addSubview:btnDoneclick];
EmpInDepTableView=[[UITableView alloc]initWithFrame:CGRectMake(160, 284,0, 0)];
[EmpInDepTableView setDelegate:self];
[EmpInDepTableView setDataSource:self];
[EmpInDepTableView setHidden:NO];
[EmpInDepTableView setBackgroundColor:[UIColor brownColor]];
EmpInDepTableView.layer.borderWidth=3.0f;
EmpInDepTableView.layer.cornerRadius=10.0f;
[self.view addSubview:EmpInDepTableView];
self.tabBarController.tabBar.userInteractionEnabled=NO;
UITableViewCell *cell=[Dept_TableView cellForRowAtIndexPath:indexPath];
DeptId=[Dep_Detail fetchDeptId_DeptName:cell.textLabel.text];
arrEmp_Detail = [[Emp_Detail fetchEmp_By_DeptId:DeptId]mutableCopy];
[UITableView animateWithDuration:0.6 animations:^
{
[EmpInDepTableView setFrame:CGRectMake(0, 64,320, 430)];
[btnDoneclick setFrame:CGRectMake(0,490,320,29)];
}completion:^(BOOL finished)
{
[EmpInDepTableView reloadData];
}];
}
else if (isEmpSelected)
{
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor brownColor]];
return view;
}
-(IBAction)btnDoneclicked:(id)sender
{
[EmpInDepTableView reloadData];
isDeptSelected=true;
isEmpSelected=false;
[btnDoneclick setHidden:YES];
self.tabBarController.tabBar.userInteractionEnabled=YES;
[UITableView animateWithDuration:0.6 animations:^
{
[EmpInDepTableView setFrame:CGRectMake(160, 284, 0, 0)];
[btnDoneclick setFrame:CGRectMake(160, 284, 0, 0)];
[self.view setBackgroundColor:[UIColor brownColor]];
}completion:^(BOOL finished)
{
}];
}

Resources