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];
}
Related
In single screen I have added UITableview and UICollectionview. I have added default delegate and datasource methods for both TableView and Collection View. but UITableView only little slow to load data. That data I have added statically that too load very slowly. Same code working fast in iPhone but iPad I am facing this slowness issues. Anyone know about this please help me do this.
I have found solution for that, Actually in my tableviewcellforrowatindexpath function i made some changes .
My current code following as,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath{
static NSString *cellId = #"SimpleTableId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell1 == nil) {
cell1 = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell2 == nil) {
cell2 = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
if(values == 1)
{
return cell;
}
else if(values == 2)
{
return cell1;
}
else
{
return cell;
}
}
I have changed like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath{
static NSString *cellId = #"SimpleTableId";
if(values == 1)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
return cell;
}
else if(values == 2)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
return cell;
}
else
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
return cell;
}
}
Now my slowness issues solved as much expected.
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.
When i select index 0 or 1 or any else that same time it also selected index according to above index
i need solution
there are many rows and when i select cell 1 same time another cell selected automatically..
#property (nonatomic, retain) NSIndexPath * checkedIndexPath;
in ViewDidLoad
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];//Default 1st row will be checkMarked
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSimpleCell];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSimpleCell]autorelease];
}
if (self.checkedIndexPath== indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *currentcell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
currentcell.accessoryType = UITableViewCellAccessoryNone;
self.checkedIndexPath = indexPath;
UITableViewCell *currentcell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
currentcell.accessoryType = UITableViewCellAccessoryCheckmark;
}
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
}
}
I want to apply checkmark as accessory type to a selected item in a uitable view. but the checkmark appears for all reused cells. How can we avoid this problem?
code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
// Configure the cell...
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell;
}
Try this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell; }
Hope this will help you.
you have to add the else case to unmark the cells that you don't want to be marked, like this:
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
Try it this way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell;
}