How to create the following UITableView in a VC with 2 Columns? As UITableView only has one column. Is it possible to do it in StoryBoard? How to add shadow, round the corners and add the blurish borders as in the pic? All help or pointer is greatly appreciated.
Edited
How to set the 2 columns in Objective C? I can't find any help over the net especially for objective C. I think both the columns are UILabel.
As for the rows, do I need to set the Prototype Cell to 3 at Story Board?
The 1st column will have static values: NAME, DATE OF BIRTH, LOCATION. The 2nd column will fetch data from an array [John Doe, 06.03.1991, New York]
Do I create a loop to insert the data?
ANSWER
I followed this link and managed to create 2 custom UILabel columns and insert the columns from NSMutableArray
I can't get the following suggested answer to work, the answer suggested that we create an Empty User Interface and then add a Table View Cell and associate it back to the VC. I can't get it to work.
Mine was just creating the 2 labels at the prototype cell at the TableView and then following the link above.
create a customcell and put 7 UILabel first 4 label gives name basic setting,NAME,DOB,LOCATION than remain 3 label set blank and give tham property in cell.h file
#pragma mark tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [yourdataarray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"yourcustomcellname"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"yourcustomcellname" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
id object = [yourdataarray objectAtIndex:indexPath.row];
if ([object isKindOfClass:[NSMutableArray class]]) {
NSMutableArray *Array = (NSMutableArray *)object;
//your cell property use here for fatch like below
[cell.NAME setText:[NSString stringWithFormat:#"%#",[Array objectAtIndex:i]]];//here i=which index your array contain name data
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return yourcustomcellheight;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
refer this tutorial for customcell
https://www.appcoda.com/customize-table-view-cells-for-uitableview/
You need to use custom UITableViewCell, it seems the first column is just a label and the second column is a textfield.
In order to add boundary and add corner
myTableViewCell.contentView.layer.borderColor = [[UIColor grayColor] CGColor];
myTableViewCell.contentView.layer.borderWidth = 1;
myTableViewCell.contentView.layer.cornerRadius = 5.0;
myTableViewCell.contentView.clipsToBounds = YES;
myTableViewCell.contentView.layer.masksToBounds = YES;
check following example as a reference to learn how to create Custom UITableViewCell and use.
https://www.appcoda.com/ios-programming-customize-uitableview-storyboard/
These columns you need to design in UITableViewCell. It can be anywhere of your preference, storyboard or xib.
For storyboard your view will be like this.
you need to add tags for all the components you add into the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
...
...
UILabel *label1 = (UILabel *)[cell viewWithTag:100];
label1.text = #"NAME";
UILabel *label2 = (UILabel *)[cell viewWithTag:200];
label2.text = #"John";
...
...
return cell;
}
The same way if you create xib your custom cell should be like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
StackTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = (stackTableviewCell*)[[[NSBundle mainBundle] loadNibNamed:#"StackTableviewCell" owner:self options:nil] objectAtIndex:0];
}
...
...
label1.text = #"NAME";
label2.text = #"John";
...
...
}
For cell UI
self.lable1.backgroundColor = [UIColor lightGrayColor];
self.lable1.layer.borderColor = [UIColor colorWithRed:169/255.0 green:169/255.0 blue:169/255.0 alpha:0.8].CGColor;
self.lable1.layer.borderWidth = 1;
self.lable1.layer.cornerRadius = 5;
self.lable1.clipsToBounds = YES;
self.lable1.layer.masksToBounds = YES;
I'm working on an iOS storyboard app with a UITableView and dynamic cells. I want a segmented control to determine which type of cell populates the tableView contents.
Here is The viewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
[_reelsOrAllSegmentedControl addTarget:self
action:#selector(segmentedControlTouched)
forControlEvents:UIControlEventValueChanged];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier0 = #"cellType1";
NSString *CellIdentifier1 = #"cellType2";
long row = [indexPath row];
if([_segmentedControl isEnabledForSegmentAtIndex:0])
{
CellType1 *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier0
forIndexPath:indexPath];
NSArray *folderArray = [[NSArray alloc] initWithArray:[[UserSession sharedSession] userFolders]];
[[cell titleTextView] setText:(NSString*)folderArray[row][#"title"]];
else if([_segmentedControl isEnabledForSegmentAtIndex:1]){
CellType2 *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier1
forIndexPath:indexPath];
NSArray *postArray = [[NSArray alloc] initWithArray:[[UserSession sharedSession] userPosts]];
cell.captionTextView.text = postArray[row][#"caption"];
return cell;
}
-(void)segmentedControlTouched
{
NSLog(#"touch");
[self.tableView reloadData];
}
Every time I switch the segmented control, the first block of the if is run. Shouldn't the second one be run when the table is refreshed?
You would be better off creating two separate classes implementing UITableViewDataSource, and setting the table view's dataSource appropriately when the user changes the selected segment.
But anyway, you can get your code working by looking at the control's selectedSegmentIndex property:
if(_segmentedControl.selectedSegmentIndex == 0) {
CellType1 *cell = [tableView
... etc.
I am trying to set the label that is within a table view cell. I created an array of all the timers that I need in each cell. When I go to construct the cell and hook up the timer label it gives me the unrecognized selector sent to instance error. I have tried everything and can not figure out why I keep getting this. The updateTimerLabel method just returns the timerLabel value so I do not see what the issue is. Can someone help me find out how to fix this?
the error is on the timerLabel
the timersArray holds an array of all created timers.
Here is the cellForRowAtIndexPath method:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TimerViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"TimerViewCell"];
[[NSBundle mainBundle] loadNibNamed:#"TimerViewCell" owner:self options:nil];
tempCell = [timersArray objectAtIndex:indexPath.row];
cell = tempCell;
tempCell = nil;
cell.timerLabel.text = [cell updateTimerLabel];
return cell;
}
here is the TimerViewCell.h
#import <UIKit/UIKit.h>
#import "Timer.h"
#interface TimerViewCell : UITableViewCell
{
IBOutlet UILabel* timerLabel;
Timer* _timer;
}
#property (strong, nonatomic) IBOutlet UILabel* timerLabel;
- (id)initCellWithTimer:(Timer*)timer;
- (IBAction)startTimerButton;
- (NSString*)updateTimerLabel;
#end
Your need to allocate cell as follows
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TimerViewCell* tempCell = [tableView dequeueReusableCellWithIdentifier:#"TimerViewCell"];
if(tempCell == nil){
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"TimerViewCell" owner:self options:nil];
tempCell = [objects firstObject];
}
tempCell.timerLabel.text = [cell updateTimerLabel];
return tempCell;
}
since you are loading cell from xib. You check your outlet connection of your timerLabel.
and also check is your cell pointing to TimerViewCell? To check click on cell and check is class like image
Here's what's happening in the following three lines of your code:
tempCell = [timersArray objectAtIndex:indexPath.row];
cell = tempCell;
tempCell = nil;
You've assigned an element from timersArray to your class variable tempCell
Next you've pointed your TimerViewCell instance cell to this element.
Finally you remove the pointer that tempCell was holding by assigning it to nil, and since cell is pointed at tempCell, then you are also pointing cell at nil
Then in the next line, you attempt to call updateTimerLabel on nil
cell.timerLabel.text = [cell updateTimerLabel];
I think you'll have more luck with something like this:
- (void)viewDidLoad
{
[self.tableView registerNib:[UINib nibWithNibName:#"TimerViewCell" bundle:nil] forCellReuseIdentifier:#"TimerViewCell"];
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TimerViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"TimerViewCell"];
cell.timerLabel.text = [cell updateTimerLabel];
return cell;
}
Well unlike java in ObjC pointers (direct memory locatioin reference) existing and anything you declare with *_instance style will be a pointer.
So when we are assigning one pointer to another they just refer to same location and changing pointer reference to a difreent location (null here) will aparently clean other as well.
tempCell = [timersArray objectAtIndex:indexPath.row];
cell = tempCell;
tempCell = nil;
after these three lines even cell will be pointing to a nil. and hence returning nil to cell which is causing crash.
So removing the line tempCell = nil; should solve the issue.
I am beginning with iOS have a silly problem. I just want to show a TableView populated with strings that are stored in a NSMutableArray. I can see that the strings are in the array, but for some reason the TableView is not showing them.
I have bascially this:
#interface Test ()
#property (weak, nonatomic) IBOutlet UITableView *contactList;
#property (strong, nonatomic) NSMutableArray *contactsArray;
#end
- (void)onContactFound:(NSString*)contact
{
[self.contactsArray addObject:contact];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.contactsArray count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//5
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.contactsArray objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
I think the problem is in the last part. I copied this code from an example (http://www.codigator.com/tutorials/ios-uitableview-tutorial-for-beginners-part-1/) that said I should add some dynamic properties but in my TableView I do not have these properties in the attributes inspector so basically I do not have the #"SettingsCell" so I guess this is one of the problems at least, maybe this code does not apply in my case and it should be done in another way?
I think you are trying to dequeue a cell without ever creating cells. I think you only get nil cells back. You should use something like this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
Also have a look in the API documentation which states:
dequeueReusableCellWithIdentifier: Returns a reusable table-view cell object located by its identifier.
Return Value: A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.
dequeueReusableCellWithIdentifier:
Does anyone have suggestions on how to purge a cached UITableViewCell?
I'd like to cache these cells with reuseIdentifier. However, there are times when I need to delete or modify some of the table rows. I expect to call reloadData after the row changes.
Right now, dequeueReusableCellWithIdentifier always returns the cached(obsolete) entry from before. How do I indicate that the cache is stale and needs to be purged?
I'm not sure why you're trying to purge cells in the first place. Every time you dequeue a cell, you need to re-set any data that needs to be displayed. The caching just prevents you from having to set up any non-changing properties every time. But the actual data that's being displayed must be set, even if the cell was cached before.
Note that your reuse identifier is supposed to be the same for all the cells of the same type. If you're doing something silly like calculating the identifier based on the row in question, then you're doing it wrong.
Your code should look something like
- (UITableViewCell *)tableView:(UITableView *)view cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
// no cached cell, create a new one here
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
// set any properties that all such cells should share, such as accessory, or text color
}
// set data for this particular cell
cell.textLabel.text = #"foo";
return cell;
}
In that example, note how I always set the data for the cell every single time, and all cells share the same identifier. If you follow this pattern, you should have no reason at all to try and "purge" your cells, as any old data will be overwritten anyway. If you have multiple types of cells, you may want to use multiple identifiers in that case, but it's still 1 identifier per cell type.
I don't know how to purge the cache, however, I use a workaround how to handle the situation, when the rows need to be changed.
First of all, I don't use static cell identifier. I ask the data object to generate it (pay attention to [myObject signature], it provides a unique string describing all needed properties):
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyObject *myObject = _dataSource[indexPath.row];
NSString *cellId = [myObject signature];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
[myObject configureCell:cell];
}
[myObject updateCellWithData:cell];
return cell;
}
[myObject signature] provides different signatures for different list of properties. So, if my object is changed, I just call [self.tableView reloadData], myObject will provide a new signature, and the table load a new cell for it.
[myObject configureCell:cell] places all needed subviews to the cell.
[myObject updateCellWithData:cell] updates the subviews of the cell with current data.
while ([tableView dequeueReusableCellWithIdentifier:#"reuseid"]) {}
I have found a really good method.
In .h
//add this
int reloadCells;
In .m
- (void)dumpCache {
reloadCells = 0;
[tableView reloadData];
}
-(void)tableView:(UITableView *)tableView cellForRowAtUndexPath:(NSIndexPath *)indexPath {
static NSString *CellID = #"Cell"
UITableViewCell *cell = [tableView dequeCellWithReuseIdentifier:CellID];
if (cell == nil || reloadCells < 12) {
cell = [[UITableViewCell alloc] initWithFormat:UITableViewCellStyleDefault reuseIdentifier:CellID];
reloadCells ++;
}
cell.textLabel.text = #"My Cell";
return cell;
}
I had to something similar today. I have a gallery of images, and when the user deletes them all, the gallery needs to get rid of the last queued cell and replace it with a "gallery empty" image. When the delete routine completes, it sets a 'purge' flag to YES, then does a [tableView reloadData]. The code in cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (purge) {
cell = nil;
purge = NO;
}
if (cell == nil) {
//*** Do usual cell stuff
}
return cell
}
I am coding ObjC (manual reference counting) and found some strange behavior that prevents UITableView and UITableViewCells from getting released, although my memory management is correct. UITableView and UITableViewCell seem to retain each other. And yes, I found a way to force the UITableView and its cells to release each other (remove cached cells).
To do this, basically you ask a table view for a reusable cell. The table view will remove it from its reusable cells cache. After all you tell that cell to remove from its superview. Done.
Now as a stand-alone class... First, you need an array of all cell identifiers you used. Next, you implement a dummy data source and let that data source clear the UITableView by reloading itself with the "ClearAllCachedCells" Data source:
#import <UIKit/UIKit.h>
#interface ClearAllCachedUITableViewCellsDataSource : NSObject <UITableViewDataSource, UITableViewDelegate>
+(void)clearTableView:(UITableView*)tv
reuseIdentifiers:(NSArray<NSString*>*)cellIdentifiers
delegateAfterFinish:(id<UITableViewDelegate>)dg
dataSourceAfterFinish:(id<UITableViewDataSource>)ds;
#end
And the magic happens in the .m file:
#import "ClearAllCachedUITableViewCellsDataSource.h"
#interface ClearAllCachedUITableViewCellsDataSource () {
BOOL clearing;
}
#property (nonatomic, readonly) UITableView *tv;
#property (nonatomic, readonly) NSArray<NSString*> *identifiers;
#property (nonatomic, readonly) id ds;
#property (nonatomic, readonly) id dg;
#end
#implementation ClearAllCachedUITableViewCellsDataSource
-(void)dealloc {
NSLog(#"ClearAllCachedUITableViewCellsDataSource (%i) finished", (int)_tv);
[_tv release];
[_ds release];
[_dg release];
[_identifiers release];
[super dealloc];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
[self performSelectorOnMainThread:#selector(clear) withObject:nil waitUntilDone:NO];
NSLog(#"TV (%i): reloading with zero cells", (int)_tv);
return 0;
}
-(void)clear {
if (!clearing) {
clearing = YES;
for (NSString *ident in self.identifiers) {
UITableViewCell *cell = [_tv dequeueReusableCellWithIdentifier:ident];
while (cell) {
NSLog(#"TV (%i): removing cached cell %#/%i", (int)_tv, ident, (int)cell);
[cell removeFromSuperview];
cell = [_tv dequeueReusableCellWithIdentifier:ident];
}
}
self.tv.delegate = self.tv.delegate == self ? self.dg : self.tv.delegate;
self.tv.dataSource = self.tv.dataSource == self ? self.ds : self.tv.dataSource;
[self release];
}
}
+(void)clearTableView:(UITableView*)tv
reuseIdentifiers:(NSArray<NSString*>*)cellIdentifiers
delegateAfterFinish:(id<UITableViewDelegate>)dg
dataSourceAfterFinish:(id<UITableViewDataSource>)ds {
if (tv && cellIdentifiers) {
NSLog(#"TV (%i): adding request to clear table view", (int)tv);
ClearAllCachedUITableViewCellsDataSource *cds = [ClearAllCachedUITableViewCellsDataSource new];
cds->_identifiers = [cellIdentifiers retain];
cds->_dg = [dg retain];
cds->_ds = [ds retain];
cds->_tv = [tv retain];
cds->clearing = NO;
tv.dataSource = cds;
tv.delegate = cds;
[tv performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
#end
The old data from previous usage of the cell should be cleared with the message prepareForReuse. When a cell is dequeued this message is sent to the cell before it is returned from dequeueReusableCellWithIdentifier:.
No way, I think...
I used a completely different approach. Instead of relying on UITableView cache, I build my own. In this way I have perfect control on when and what to purge.
Something like this...
given:
NSMutableDictionary *_reusableCells;
_reusableCells = [NSMutableDictionary dictionary];
I can do:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"whatever-you-need";
UITableViewCell *cell = [_reusableCells objectForKey:cellIdentifier];
if (cell == nil)
{
// create a new cell WITHOUT reuse-identifier !!
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// store it in my own cache
[_reusableCells setObject:cell forKey:cellIdentifier];
/* ...configure the cell... */
}
return cell;
}
and:
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[_reusableCells removeAllObjects];
}
Hope this may help.