UiTableViewCell is not displaying in Storybord - ios

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.

Related

How to get multiple selected row value from uitableview and save multiple selected value in array? [duplicate]

This question already has answers here:
Select multiple rows in UITableview [duplicate]
(5 answers)
Closed 6 years ago.
I have list of states in UITableView.Now i want to select multiple row of UITableView and wanna get this selected row values in one array.how can i get this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSMutableArray *arrData =[statearray objectAtIndex:indexPath.row];
NSLog(#"arrdata>>%#",statearray);
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [statearray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
You can call tableview.indexPathForSelectedRows. This will output an array with the indexpaths for all selected rows in your tableview!
This is how your code would look like in swift 3:
var values : [String] = []
let selected_indexPaths = tableView.indexPathsForSelectedRows
for indexPath in selected_indexPaths! {
let cell = tableView.cellForRow(at: indexPath)
values.append((cell?.textLabel?.text)!)
}
After running this code the values of all selected cells should be in the values array.
you can keep track of selected cells with below way
var selectedCells: [UITableViewCell] = []
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedCells.append(tableView.cellForRow(at: indexPath)!)
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let deselectedRow = tableView.cellForRow(at: indexPath)
if(selectedCells.contains(deselectedRow!)){
let indx = selectedCells.index(of: deselectedRow!)
selectedCells.remove(at: indx!)
}
}
Idea is to maintain array of selected cells when cell selection happens and remove cell once deselection is done
Best way is to get selected indexpaths using method
tableView.indexPathsForSelectedRows
you can get the data out in an array
var oldArray: [NSIndexPath] = [NSIndexPath.init(row: 0, section: 0), NSIndexPath.init(row: 1, section: 0), NSIndexPath.init(row: 2, section: 0), NSIndexPath.init(row: 3, section: 0)]
var newArray: [Int] = oldArray.flatMap { ($0.row) }
Like if we have array in form of oldArray, We can get only rows using flatMap.
There is default method of tableview,
self.tableView.indexPathsForSelectedRows
Which gives you an array of selected indexpaths. But you need to also set property of tableview
self.tableView.allowsMultipleSelection = true
If want back selcted rows to track,
NSArray *selectedRowsArray = [yourTableViewName indexPathsForSelectedRows];
Answer updated
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//the below code will allow multiple selection
if ([yourMutableArray containsObject:indexPath])
{
[yourMutableArray removeObject:indexPath];
}
else
{
[yourMutableArray addObject:indexPath];
}
[yourTableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if ([yourMutableArray containsObject:indexPath])
{
//Do here what you wants
if (contentArray.count > 0) {
NSDictionary *dictObje = [contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[dictObje objectForKey:#"name"]];
}
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
//Do here what you wants
if (contentArray.count > 0) {
NSDictionary *dictObje = [contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[dictObje objectForKey:#"name"]];
}
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}

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 set section headers without repeating data in tableview 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.

UITableView cell allocate mark when checked...it would checked automatically when scrolling tblview

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;
}

Removing UITableViewCell Selection

Currently I'm overriding the standard UITableViewSelectionStyle by using UITableViewSelectionStyleNone and then changing the color the cell based on delegate methods:
- (void)tableView:(UITableView *)tableView
didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor yellowColor]];
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor whiteColor]];
}
- (void)tableView:(UITableView *)tableView
didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"indexpath: %i",indexPath.row);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor whiteColor]];
}
- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor whiteColor]];
}
This almost works except that whenever I highlight a cell and then drag my finger off of it without actually selecting it, the color doesn't change to white...if I set it to [UIColor RedColor] it works perfeclty. Why is this...
Edit:
Somehow when I print out the indexPath.row after didUnhlightRowAtIndexPath I get "indexpath: 2147483647" from my NSLog
You could try:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if you just want the highlight to go away after selecting a cell. Unless I misunderstand your question.
You can also try this
tableView.allowsSelection = NO;
another way of doing this
cell.selectionStyle = UITableViewCellSelectionStyleNone;
one more
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You can also do it from the storyboard. Select the tableViewCell and under Attributes Inspector you can choose Selection > None
Here is a Swift 2 version
if let indexPaths = self.tableView.indexPathsForSelectedRows {
for indexPath in indexPaths {
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
Unselect selected row(s). You do not even need to know which one it is.
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
By maintaing a local instance of my indexPath, I was able to find the last selected cell and change its color to whitecolor. Seems really annoying that I have to maintain state myself, but it is what it is...
How about doing this with an extension?
import UIKit
extension UITableView {
func removeRowSelections() {
self.indexPathsForSelectedRows?.forEach {
self.deselectRow(at: $0, animated: true)
}
}
}
Usage:
tableView.removeRowSelections()
Simplest solution that worked for me (Swift 4.2)
(if you still want table view's row to be selectable)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let index = self.tableView.indexPathForSelectedRow{
self.tableView.deselectRow(at: index, animated: true)
}
}

Resources