Create TableView in some UIView, Objective c - ios

In my ViewController I have a little UIView. How do I programmatically create a TableView instead of this "blue" UIView.
I have this code for my UIView.
self.viewForTableView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2,
self.navigationController.navigationBar.frame.size.height,
self.view.frame.size.width/2,
self.view.frame.size.height)];
self.viewForTableView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.viewForTableView];
That's how I would like to have it preview.

Swift
let tblVW:UITableView = UITableView(frame: viewForTableView.frame)
viewForTableView.addSubview(tblVW)
tblVW.delegate = self
tblVW.dataSource = self
Objective-c
UITableView *tblView = [[UITableView alloc]initWithFrame:viewForTableView.frame];
[viewForTableView addSubview:tblView];
tblView.delegate = self;
tblView.dataSource = self;
you need to implement following methods:
#pragma mark - Table View Data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 0//number rows you want in table
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //by default is 1
}

viewcontroller.h
#interface yourViewController ()<UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *tbleView;
}
Viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
tbleView.delegate=self;//for delegate methods
tbleView.datasource=self;//for datasource methods
tbleView.backgroundColor=//yourColor
tbleView.frame=CGRectMake(//your frame);
[yourView addSubview tbleView];
}

Related

iOS cell accessoryView set the same view stuck

The source codeļ¼š
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
- (UITableView *)tableView
{
if (!_tableView) {
CGRect frame = self.view.bounds;
frame.size.height -= (64 + 40);
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.separatorColor = __color_ListSepatator;
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.showsVerticalScrollIndicator = NO;
_tableView.showsHorizontalScrollIndicator = NO;
}
return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45.0f;
}
/*Here is dalegate*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string_id = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string_id];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string_id];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
/*Here is jammed*/
cell.accessoryView = self.viewccessory;
return cell;
}
/*Here is propety*/
- (UIView *)viewccessory
{
if (!_viewccessory) {
_viewccessory = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
_viewccessory.backgroundColor = [UIColor orangeColor];
}
return _viewccessory;
}
Question
When different cell repeat the same accessoryView Settings, getting stuck when I was out of the controller, can't into the interface
Each time when the controller card dead don't know why.Why can't accessoryView set the same view?Ask everybody to help. Thanks
accessoryView for each tableview cell should be independent but not shared, you should make a new view very time when assigning to table view cell.
Why acessoryView can't be shared? Each view should only has one superview.

UITableViewCell Separator disappearing in iOS9

I've created a custom UITableViewCell and i need a bottom line separator for each cell then i put the separator in init method of my custom tableViewCell
This is my TableView code :
#import "Telegram_TableViewController.h"
#import "Custom_TableViewCell.h"
#interface Telegram_TableViewController ()
#end
#implementation Telegram_TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.array =[[NSMutableArray alloc] init];
for (int i=0; i<200; i++) {
[self.array addObject:[NSString stringWithFormat:#"item %i" ,i]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.array count] ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *Identifier = #"CELL";
Custom_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
cell = [[Custom_TableViewCell alloc] init];
UILabel *txt = [[UILabel alloc]init];
txt.text = [NSString stringWithFormat:#"Item " ] ;
cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
return cell;
}
and my custom TableViewCell code :
#import "Custom_TableViewCell.h"
#implementation Custom_TableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(id)init
{
self = [super init];
if (self) {
UIEdgeInsets insets = self.separatorInset;
self.separatorInset = UIEdgeInsetsMake(0.0, 0.0,insets.bottom + 6.0, 0.0);
self.separatorInset = insets;
}
return self;
}
There is no error but it doesn't work. How can i fix it?
Thanks in advance.
Clean init method in Custom_TableViewCell and see if this helps.
In Telegram_TableViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *Identifier = #"CELL";
Custom_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
cell = [[Custom_TableViewCell alloc] init];
UILabel *txt = [[UILabel alloc]init];
txt.text = [NSString stringWithFormat:#"Item " ] ;
cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[tableView setSeparatorColor:[UIColor blackColor]];
return cell;
}
And make sure the color of the separator is different than the color of the cell itself.
Use this code in your cell,s .h file
#property (nonatomic,strong) UIView* cellSeperatorView;
and in .m file
-(UIView *)cellSeperatorView{
if (!cellSeperatorView) {
self.cellSeperatorView = [[UIView alloc] initWithFrame:CGRectZero];
[self.cellSeperatorView setBackgroundColor:[UIColor lightGrayColor]];
[self addSubview:self.cellSeperatorView];
}
return cellSeperatorView;
}
-(void)prepareForReuse{
self.cellSeperatorView = nil;
}
-(void)layoutSubviews{
[super layoutSubviews];
[self.cellSeperatorView setFrame:CGRectMake(0, 0, 500, 1)];
}
and call this method in your tableview class in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)..
UITableViewCellClass *cell = (UITableViewCellClass *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[UITableViewCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.cellSeperatorView.hidden = (indexPath.row == ([self.yourtableViewArray numberOfRowsInSection:indexPath.section]));
and in you viewdidload method add this
[self.yourtableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
i think this can help you for making separator..
You'd better register in the method viewDidLoad when you want to custom cell. [self.tableView registerClass:[Custom_TableViewCell class] forCellReuseIdentifier:Identifier];
What is cell = [[Custom_TableViewCell alloc] init];? You are not reusing the cell, but initiate every time. Just do not write this line.
The right way to overwrite the init method of tableViewCell to is rewrite - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
Now you can see the tableView correctly, but I do not understand a bottom line separator, just write what you want in the method initWithStyle.

Load Array in UITableView Cell

I want to make paging swipe left and right using UIScrollView after view detailController.
First, main.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
OZDetailViewController *detailViewController = [[OZDetailViewController alloc] initWithNibName:#"OZDetailViewController" bundle:nil];
detailViewController.arrDetailNews = [_arrNews objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
OZDetailViewController *arrNewsAll = [[OZDetailViewController alloc] initWithNibName:#"OZDetailViewController" bundle:nil];
arrNewsAll.allNewsArray = _arrNews;
[self.navigationController pushViewController:arrNewsAll animated:YES];
}
When I selected content in tableviewcell, arrDetailNews can loaded in method viewDidLoad() and cellForRowAtIndexPath(). But arrNewsAll cannot loaded in method cellForRowAtIndexPath().
This is my detailViewController.h:
#property (nonatomic, copy) NSArray *allNewsArray;
And detailViewCOntroller.m:
#synthesize allNewsArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor clearColor];
NSLog(#"dataArray: %#", allNewsArray);
}
- (int)numberINSectionsInTableView: (UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"dataArrayCell: %#", allNewsArray);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"New Cell"];
}
if(indexPath.row != self.arrDetailNews.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor whiteColor];
[cell addSubview:line];
}
tableView.allowsSelection = NO;
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
if (indexPath.row==0) {
cell.textLabel.text = #"1st";
}
if (indexPath.row==1) {
cell.textLabel.text = #"2nd";
}
if (indexPath.row==2) {
cell.textLabel.text = #"3rd";
}
return cell;
}
If allNewsArray can loaded in cellForRowAtIndexPath() I can continue next step for paging with UIScrollView. Note, numberOfRowsInSection I set to 4 because I need 4 rows (custom view).
Assuming you setup your delegate & dataSource outlets correctly from xib/storyboard, you still need to specify the number of rows per section (or number of sections).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return allNewsArray.count;
}
Alternatively, the method for number of sections is:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

Accessibility: Embedding a UITableView inside the Footer of a UITableView

I am trying to reuse an existing UITableViewController by embedding a UITableView in the footer of an existing table.
The premise is that on selection of a row, the footer will appear with a list of further, related options. This works fine but the accessibility inspector is unable to read the rows in the table embedded in the footer.
I created a quick sample of code to show the issue.
Code for the main UITableViewController
#interface MyUITableViewController ()
#end
#implementation MyUITableViewController{
MySubUITableViewController *dataSourceClass;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dataSourceClass = [[MySubUITableViewController alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
if(section == 0){
CGRect tableFrame = CGRectMake(0, 0, self.view.frame.size.width, 200);
UITableView *view = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
view.rowHeight = 30;
view.scrollEnabled = YES;
view.showsVerticalScrollIndicator = YES;
view.userInteractionEnabled = YES;
view.bounces = YES;
view.delegate = dataSourceClass;
view.dataSource = dataSourceClass;
return view;
}
return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if(section == 0)
return 200;
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"identifier";
UITableViewCell *cell;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"Section %d Row %d", indexPath.section, indexPath.row];
return cell;
}
The code for the table to embed within the footer
#implementation MySubUITableViewController {
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"subCell";
UITableViewCell *cell;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"Footer Row %d", indexPath.row];
return cell;
}
#end
Anyone know how to get round the Footer rows not being read by accessibility?
I tried
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}
but to avail.
Thanks
Ok, so the only solution I could get to work, is to create a custom UITableViewCell which implements the UITableViewDataSource, UITableViewDelegate delegates.
Effectively creating a table inside a UITableViewCell.
Something along the lines of:
#interface MyTableWrappingCell() <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, weak) UITableView *myTableView;
#end
#implementation MyTableWrappingCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
myTableView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
myTableView.dataSource = self;
myTableView.delegate = self;
[self.contentView addSubview:myTableView];
self.myTableView = myTableView;
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"subCell";
UITableViewCell *cell;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"internal Row %d", indexPath.row];
return cell;
}
- (void) forceLoad{
[self.myTableView reloadData];
}
#end
Accessibility is then able to read the contents.

UITableView for iOS

I am very new to Objective-C. I just searched and tried to learn about UITableView.
I have created this UITableView. instead of "a" in all the rows i want it to be in a sequence like "a b c d..." and if i increase the no of rows it should scroll. its not scrolling so here is my code.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
CGRect frame = self.view.frame;
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.backgroundColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellIDent"];
cell.textLabel.text = #"a";
return cell;
}
#end
set table view Delegates in IB or if u created Programmatically then in viewDidLoad Take one array fill it Dynamically or Statically.Give array count in NumberOfRowsInsection method and reload table view at appropriate place
in your cellForRowAtIndexPath: add these lines
char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:#"%c" , ch];
Check this tutorial, I'm sure it will help you
UItableView Guide
and for scrolling, You don't need to do anything yourselves, as the number of cells increases beyond the provided size, it automatically becomes scrollable and make sure that scrolling is enabled in your UITableView attribute inspector, check the image
Happy Coding
Regards
Use below code.....define alphabetarr in globally then use it in code...
- (void)viewDidLoad
{
alphabetarr = [[NSArray alloc] initWithObjects:#"a",#"b",#"c",#"d",#"e",#"f",#"g",#"h",#"i",#"j",#"k",#"l",#"m",#"n",#"o",#"p",#"q",#"r",#"s",#"t",#"u",#"v",#"w",#"x",#"y",#"z", nil];
CGRect frame = self.view.frame;
UITableView *tableview = [[UITableView alloc] initWithFrame:frame];
tableview.backgroundColor = [UIColor cyanColor];
tableview.separatorColor = [UIColor blackColor];
tableview.delegate = self;
tableview.dataSource = self;
[self.view addSubview:tableview];
[super viewDidLoad];
// 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 [alphabetarr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell!=nil) {
cell = nil;
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [alphabetarr objectAtIndex:indexPath.row];
return cell;
}

Resources