How to set section headers without repeating data in tableview iOS? - ios

I have a table view and now I'm initialising the table like,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arr count]+1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arr count]+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == 0)
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
return cell;
}
else
{
static NSString *CellIdentifier = #"moduleCell";
MyTableViewCell *cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#""]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
current = [arr objectAtIndex:indexPath.row-1];
[cell setCellDetails:arr WithIndex:row-1 withParentView:self];
return cell;
}
}
When I'm doing this data repeating in my tableview. Before I do this, I did,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
This is working but I need to set section headers like titles. So is there a way to set section headers without repeating data?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0){//for first section
}else if(indexPath.section == 1){//for second section
}
}

You need to implement this method:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section==0) {
return "name";
}else{
//
}
}

You need to keep heading in Array. So, you can avoid hardcoded in the delegate method.
NSArray *heading=#[#"Today",#"Yesterday",#"Tomorrow"];
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *heading = [heading objectAtIndex:section]; //
return heading;
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var cstmView:UIView!
let lbl:UILabel
cstmView = UIView()
cstmView.frame = CGRectMake(0,0,tableView.contentSize.width,70.0)
let logo:UIImageView = UIImageView();
logo.image = UIImage(named: "logo_with_lightGrayColor")
logo.frame = CGRectMake(8.0,10.0,200.0,60.0)
lbl = UILabel()
lbl.frame = CGRectMake(8.0,80.0,200.0,20.0)
lbl.text = "USER NAME"
lbl.textColor = UIColor.whiteColor()
lbl.font = UIFont.systemFontOfSize(15.0)
cstmView .addSubview(logo)
cstmView.addSubview(lbl)
return cstmView;
}
You should implement custom header for tableview.

Related

I am having a tableview with multiple cell in it and in each cell having tableview, collectionview, tableview, like that

How to handle didSelectRowAtIndexPath so that it works on all the three different cell for each cell in it?
Preview
Flow of my code
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[dataArray[indexPath.row] valueForKey:#"type"] isEqual:#"Traffic" ])
{
if(!TrafficCell)
{
TrafficCell = [tableView dequeueReusableCellWithIdentifier:#"CollectionVIewTableViewCell" forIndexPath:indexPath];
NSDictionary *dict=dataArray[indexPath.row];
TrafficCell.Traffic = [dict valueForKey:#"detail"];
[TrafficCell.collectionView reloadData];
return TrafficCell;
}
return TrafficCell;
}
else if([[dataArray[indexPath.row] valueForKey:#"type"] isEqual:#"News"])
{
if(!NewsCell)
{
NewsTableViewCell *cell = (NewsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"NewsTableViewCell" forIndexPath:indexPath];
NSDictionary *dict=dataArray[indexPath.row];
cell.News = [dict valueForKey:#"detail"];
[cell.NewsTableView reloadData];
return cell;
}
return NewsCell;
}
else
{
if(!CategoryCell)
{
CategoryCell= [tableView dequeueReusableCellWithIdentifier:#"CategoryTableViewCell" forIndexPath:indexPath];
NSDictionary *dict=dataArray[indexPath.row];
CategoryCell.Category = [dict valueForKey:#"detail"];
[CategoryCell.CategorycollectionView reloadData];
return CategoryCell;
}
return CategoryCell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
You can handle tap on UITableViewCell the same as you implement in cellForRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dict = dataArray[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];
if([dict[#"type"] isEqual:#"Traffic" ]){
//Find your collectionView in cell
//Tap on Traffic cells
}
else if([dict[#"type"] isEqual:#"News"]){
//Tap on News cells
}
else {
//Tap on other cells
}
}
And in cellForRowAtIndexPath you need
TrafficCell.collectionView.delegate = self;
CategoryCell.CategorycollectionView.delegate = self;
for handle tap on UICollectionViewCell and implement UICollectionViewDelegate method
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//Tapped on your collectionViewCell inside the uitableviewcell
}

How to create fixed amount of UITableViewCell and if the data is null then row should be not be shown

I am using tableView to display some information which is just four line of information. And i want to assign respective information to each row.
Like how shown in the below image there are four rows, same as in the image so i am using tableView for that. Here my problem is that i have created four cells but don't know how should i use label in specific cell and show the info.
and also if the value is null that row should not be there means if two values among four are null then only two rows having values should be there in tableView. How can i achieve this. Till now i am only able to show one row information only.
- (NSArray *)myTableViewCells
{
if (!_myTableViewCells)
{
_myTableViewCells = #[
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]
];
}
return _myTableViewCells;
}
if([managedObject valueForKey:#"personality_company_master_values"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Personality %#",[managedObject valueForKey:#"personality_company_master_values"]]];
}
if([managedObject valueForKey:#"video_tag"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Tag %#",[managedObject valueForKey:#"video_tag"]]];
}
if([managedObject valueForKey:#"industry_master_values"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Industry %#",[managedObject valueForKey:#"industry_master_values"]]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.myTableViewCells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = self.myTableViewCells[indexPath.row];
// NSManagedObject *managedObject = [self.devices lastObject];
cell.backgroundColor = [self colorFromHexString:#"#014455"];
cell.textLabel.text = _displayValues[indexPath.row];
cell.textLabel.backgroundColor = [self colorFromHexString:#"#014455"];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font=[UIFont systemFontOfSize:14.0];
// UILabel *lbl=(UILabel*)[cell viewWithTag:900];
// [lbl setText:[managedObject valueForKey:#"personality_company_master_values"]];
// lbl.textColor=[UIColor blackColor];
return cell;
}
Get the value you want display in an array.
Something like this
#property (monatomic, strong)NSMuatableArray *displayValues;
-(void)viewDidLoad
{
self.displayValues = [[NSMutableArray alloc]init];
NSManagedObject *managedObject = [self.devices lastObject];
if([managedObject valueForKey:#"personality_company_master_values"] != nil)
{
[self.displayValues addObject:[managedObject valueForKey:#"personality_company_master_values"]];
}
if([managedObject valueForKey:#"company_master_values"] != nil)
{
[self.displayValues addObject:[managedObject valueForKey:#"company_master_values"]];
}
if([managedObject valueForKey:#"tag_master_values"] != nil)
{
[self.displayValues addObject:[managedObject valueForKey:#"tag_master_values"]];
}
}
- (NSArray *)myTableViewCells
{
if (!_myTableViewCells)
{
_myTableViewCells = #[
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]
];
}
return _myTableViewCells;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.displayValues.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = self.myTableViewCells[indexPath.row];
// NSManagedObject *managedObject = [self.devices lastObject];
//cell.textLabel.text = [NSString stringWithFormat:#"%#",[managedObject valueForKey:#"personality_company_master_values"]];
cell.textLabel.text = self.displayValues[indexPath.row];
//not getting have to do this way or any other way please help
// secondLabel.text = [NSString stringWithFormat:#"%#",[managedObject valueForKey:#"company_master_values"]];
// thirdLabel.text = [NSString stringWithFormat:#"%#",[managedObject valueForKey:#"tag_master_values"]];
return cell;
}
I'm afraid you're doing several things wrong, starting with preallocating an array of cells. Tableviews don't work like that, you provide cells on demand and populate them with values from your data model. When you want to remove a cell update your data model then call reloadData(). Here's a simple example:
import UIKit
class MyCell: UITableViewCell {
var row: Int = -1 // serves no purpose but to show how you might subclass a UITableViewCell
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var dataModel = [
"Hello", "World,", "this", "is", "a", "tableview"
]
var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
var frame = view.bounds
let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.height
frame.origin.y += statusBarHeight
frame.size.height -= statusBarHeight
tableView = UITableView(frame: frame, style: .Plain)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(MyCell.self, forCellReuseIdentifier: "mycell")
view.addSubview(tableView)
}
// MARK: - UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataModel.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mycell") as! MyCell
let row = indexPath.row
cell.row = row // there is no point in doing this other than to show it as an example
cell.textLabel!.text = dataModel[row]
return cell
}
// MARK: - UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
dataModel.removeAtIndex(indexPath.row)
tableView.reloadData()
}
}
EDIT: Here's an objective c version
////////////////////////////
/// Objective C Version //
////////////////////////////
#import "ViewController.h"
#interface MyCell: UITableViewCell
#property(assign) NSInteger row; // serves no purpose but to show how you might subclass a UITableViewCell
#end
#implementation MyCell #end
#interface ViewController() <UITableViewDataSource, UITableViewDelegate>
#property NSMutableArray *dataModel;
#property UITableView *tableView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dataModel = [NSMutableArray arrayWithArray: #[#"Hello", #"World,", #"this", #"is", #"a", #"tableview"]];
CGRect frame = self.view.bounds;
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
frame.origin.y += statusBarHeight;
frame.size.height -= statusBarHeight;
_tableView = [[UITableView alloc] initWithFrame: frame style: UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass: [MyCell class] forCellReuseIdentifier: #"mycell"];
[self.view addSubview: _tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell *) [tableView dequeueReusableCellWithIdentifier: #"mycell"];
NSInteger row = indexPath.row;
cell.row = row; // there is no point in doing this other than to show it as an example
cell.textLabel.text = _dataModel[row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[_dataModel removeObjectAtIndex: indexPath.row];
[_tableView reloadData];
}
#end
You dont need to create a fixed amount of cells this is not an efficient solution to the problem. You should create NSMutableDictionary and save the data like this:
NSMutableArray *data = [NSMutableDictionary dictionary];
[data setValue:#"Vijayakanth" forKey:#"Personality"];
Now in the table view delegate you can return the keys count for noOfRowsInSection and in cellForRowAtIndexPath you can get the key from dictionary get the value w.r.t that key and assign the values to your cell. In your case:
Key: Personality (which is shown on the left side)
Value: Vijayakanth (which is shown on the right side)
Hope you understand the point.

UiTableViewCell is not displaying in Storybord

UiTableViewCell is not displaying in Storybord,
UiTableViewCell is not displaying in Storybord,UITableViewCell is not displaying in Storybord can any one help me Thanks in advance
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 50;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ContactsCell";
ContactsCell *cell = (ContactsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[ContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.accessoryType=UITableViewCellAccessoryCheckmark;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
Have you set Delegate and DataSource of you UITableView?
Using,
[self.IByourTableView setDelegate:self];
[self.IByourTableView setDataSource:self];
Update #1: For answer comment(ie: Open ChatViewController),
for that use delegate method of UITableView
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var storyBoardName = "Main"
var storyBoard:UIStoryboard?
storyBoard = UIStoryboard(name: storyBoardName, bundle: nil)
var gotoViewController: UIViewController? = storyBoard?.instantiateViewControllerWithIdentifier("YourViewControllerIdentifier") as? UIViewController
navigationController?.pushViewController(gotoViewController!, animated: true)
}
Note:Go to Story board and give Identifier to your ChatViewController
you wrote nothing to display in table
in cellforrow at indexpath add below line
cell.textlable.text=#"add something";
or replace your cellforrowatindexpath with below code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ContactsCell";
ContactsCell *cell = (ContactsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[ContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.accessoryType=UITableViewCellAccessoryCheckmark;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
cell.textlable.text=#"add something";
return cell;
}
Do you know that you can't make UITableViewCell appear in storyboard by writing code? All the code you wrote will only display in the device.
You want UITableViewCell to appear in storyboard then you need to open storyboard and start adding and editing your uitableviewcell not by writing code.

UITableView set multiple cell as selected?

In my table view I've two sections with different custom cell for both section.I want to select one row from each section.Is it possible?
Here is my approach -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"categoryCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ReportCategoryCell* catCell = (ReportCategoryCell *)cell;
if (indexPath == selectedCategoryIndexPath)
{
catCell.selected = YES;
}
else
{
catCell.selected = NO;
}
catCell.categoryLabel.text = [categories objectAtIndex:indexPath.row];
}
else
{
static NSString *CellIdentifier = #"durationCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ReportDurationCell* durationCell = (ReportDurationCell *)cell;
if (indexPath == selectedDurationIndexPath) {
durationCell.selected = YES;
}
else
{
durationCell.selected = NO;
}
durationCell.reportDurtionLabel.text = [durations objectAtIndex:indexPath.row];
}
cell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
and
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
selectedCategoryIndexPath = indexPath;
}
else
{
selectedDurationIndexPath = indexPath;
}
}
But it select only one row from either section(i want to select one row from each section).also i need first row of each section selected by default.
Add a bool property in your custom cell's header files. and make following changes (Assuming that you have initialised selectedCategoryIndexPath and selectedDurationIndexPath appropriately) -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"categoryCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ReportCategoryCell* catCell = (ReportCategoryCell *)cell;
if (indexPath.row == selectedCategoryIndexPath.row) {
catCell.isSelected = YES;
}
else
catCell.isSelected = NO;
catCell.categoryLabel.text = [categories objectAtIndex:indexPath.row];
catCell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
catCell.selectionStyle = UITableViewCellSelectionStyleNone;
return catCell;
}
else
{
static NSString *CellIdentifier = #"durationCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ReportDurationCell* durationCell = (ReportDurationCell *)cell;
if (indexPath.row == selectedDurationIndexPath.row) {
durationCell.isSelected = YES;
}
else
durationCell.isSelected = NO;
durationCell.reportDurtionLabel.text = [durations objectAtIndex:indexPath.row];
durationCell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
durationCell.selectionStyle = UITableViewCellSelectionStyleNone;
return durationCell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
selectedCategoryIndexPath = indexPath;
}
else
{
selectedDurationIndexPath = indexPath;
}
[self.tableView reloadData];
}

Multiple UITableview in Single Viewcontroller

I have a viewcontroller in that i want to show 3 tableviews(because the content and the table properties are different). How do i add these delegate methodes for 3 tables in one viewcontroller?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
EDIT
So what will i do if i want add a uislider to one table row using custom cell and when i slide the value i want to change the display brightness?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == _displayThemes) {
return 1;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView==_displayThemes) {
return 1;
} else {
return 5;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == _displayThemes) {
cell.textLabel.text = [displaytheme objectAtIndex:indexPath.row];
return cell;
} else {
cell.textLabel.text = [fontlist objectAtIndex:indexPath.row];
return cell;
}
}
- (IBAction)fontButton:(id)sender {
_fontList = [[UITableView alloc]init];
[self.view addSubview:_fontList];
[UIView animateWithDuration:1.5
delay:0
options: UIViewAnimationOptionTransitionCurlUp
animations:^{
_fontList.fram e= CGRectMake(0,800,320,200);
}
completion:^(BOOL finished){
_fontList.frame = CGRectMake(0,280,320,200);
}];
[_fontList reloadData];
}
- (IBAction)button:(id)sender {
_displayThemes = [[UITableView alloc]init];
[self.view addSubview:_displayThemes];
[UIView animateWithDuration:1.5
delay:0
options: UIViewAnimationOptionTransitionCurlUp
animations:^{
_displayThemes.frame=CGRectMake(0,800,320,200);
}
completion:^(BOOL finished){
_displayThemes.frame=CGRectMake(0,280,320,200);
}];
}
It will be the same as you do it with one table view, but you should check which tableview is currently using.
myTableView1.dataSource = self;
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == myTableView1) {
// your code 1
}
else
if (tableView == myTableView2) {
// your code 2
}
else
if (tableView == myTableView3) {
// your code 3
}
}
Edit:
About brightness:
How to change brightness in iOS 5 app?
And about UISlider it has minimunValue and maximumValue properties.
- (void) sliderChanged:(UISlider*)sender{
UISlider *slider = (UISlider*)sender;
[[UIScreen mainScreen] setBrightness:slider.value];
}
Edit:
slider.tag = 1;
[cell addSubview:slider];
...
// when you need..
indexPath = [NSIndexPath indexPathForRow:myRow inSection:mySecion];
UISlider* slider = (UISlider*) [[self.tableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
You always get a reference and can always check for which tableView delegate or dataSource method is called.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.tableView1)
{
return 1;
}
if (tableView == self.tableView2)
{
return 1;
}
if (tableView == self.tableView3)
{
return 1;
}
}
You don't gain anything by using same identifier for all tables. Use something like:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (tableView == self.tableView1)
{
static NSString *CellIdentifier1 = #"cellForTable1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
cell.textLabel.text = [NSString stringWithFormat: #"table1: %d.%d", indexPath.section, indexPath.row];
return cell;
}
if (tableView == self.tableView2)
{
static NSString *CellIdentifier2 = #"cellForTable2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
cell.textLabel.text = [NSString stringWithFormat: #"table2: %d.%d", indexPath.section, indexPath.row];
return cell;
}
if (tableView == self.tableView1)
{
static NSString *CellIdentifier3 = #"cellForTable3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3];
}
cell.textLabel.text = [NSString stringWithFormat: #"table3: %d.%d", indexPath.section, indexPath.row];
return cell;
}
}
//add tag in tableView .
myTable1.tag = 200;
myTable2.tag = 201;
myTable3.tag = 202;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView.tag == 200)
{
return 1;
}
if (tableView.tag == 201)
{
return 1;
}
if (tableView.tag == 202)
{
return 1;
}
}
You can manage multiple tableView in a single ViewController by
writing below code inside UItableViewDelegate and UItableViewDatasource.
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableView1
{
// place your code here
}
else if tableView == tableView2 {
// place your code here
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableView1
{
// place your code here
}
else if tableView == tableView2 {
// place your code here
}
else {
return 0
}
}
// You can set a different size of your tableView using below lines of code
if tableView == tableView1{
return 50
}
else{
return 40
}
None of the previous worked for me, I come up with the following solution:
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if((tableView1 != nil) && (tableView == tableView1)) {
return Items1.count
}
else if((tableView2 != nil) && (tableView == tableView2)) {
return Items2.count
}
else if((tableView3 != nil) && (tableView == tableView3)) {
return Items3.count
}
else {
return 0
}
}

Resources