Add left and right margin to tableview of UITableViewController - ios

I have tableview of UITableViewController with static cells.
Now I want to add left and right margin constrain at the runtime because I can not add constrain using Interface Builder.
Thanks

You could embed a UITableView inside a UIView instead of the UIViewController. If you did this you could resize the tableview and set margins in the interface builder. Create a new UIViewController (one that doesn't have a UITableView included) then drag a new UITableView onto it. Remember to set the data source and delegate to the new UIViewController. That should do it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==0){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = #"your text";
return cell;
}
if(indexPath.row==1){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = #"your text2";
return cell;
}
etc...

Not sure but May be - tableView's scrollview insets can help you-

Related

Outlets cannot connected to repeating content in uitableview

I'm using Two tableview cells on single tableview like dis...enter image description here
and when I put subviews on both cells it is showing Outlets cannot connected to repeating content in uitableview
You can access the views with the help "tag". First set tag to the required view then access it as follows,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"tCell"];
//I have set CollectionView tag to 100 and Label tag to 50
UICollectionView *collect = (UICollectionView *) [cell viewWithTag:100]; //if the view is CollectionView
UILabel *lbl = (UILabel *) [cell viewWithTag:50]; //if it is a UILabel
return cell;
}

UILabel appears multiple times in UITableViewCell

I'm currently creating my UITableViewCells programmatically like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Home-Cell" forIndexPath:indexPath];
UILabel *newLabel = [[UILabel alloc] initwithframe:cell.frame];
[newLabel setText:self.data[indexPath.row]];
[cell addSubview:newLabel];
return cell;
}
This seems to create a new UILabel each time the cell is reused though, which I definitely don't want. I tried doing the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Home-Cell" forIndexPath:indexPath];
if (cell == nil) {
UILabel *newLabel = [[UILabel alloc] initwithframe:cell.frame];
[cell addSubview:newLabel];
}
[newLabel setText:self.data[indexPath.row]];
return cell;
}
but then the UILabel seems to never be created. Perhaps this is because I'm using prototype cells with Storyboard and thus the cells are never nil?
You have two solutions.
Create a custom table view cell that already has the label.
If you want to add the label in code, don't register a class for the cell. Then the dequeued cell can be nil and you can add the label at that time (like in your 2nd set of code). This also requires using the dequeueReusableCellWithIdentifier method that doesn't also take an indexPath.
You should create a UITableViewCell subclass and add "newLabel" as a property.
The cell is never nil because the method you use to dequeue the table view cell always returns a cell, creating one if it doesn't already exist in the reuse queue.
A better solution would be to create the label in the cell prototype in the storyboard.
This implementation is against MVC architecture where controller managers stuff and do not deal with view. Here, you are trying to add stuff in view from controller. It is suggested to subclass UITableViewCell as below and add your custom UI controls in there
MyTableViewCell.h
#interface MyTableViewCell : UITableViewCell {
}
#property (nonatomic, strong) UILabel *myLabel;
#end
Then you can implement layoutSubviews in your MyTableViewCell.m file to define the look and feel of your cell.
MyTableViewCell.m
- (id)initWithStyle:(UITableViewCellStyle)iStyle reuseIdentifier:(NSString *)iReuseIdentifier {
if ((self = [super initWithStyle:iStyle reuseIdentifier:iReuseIdentifier])) {
self.myLabel = [[UILabel alloc] initWithFrame:<Your_Frame>];
// Set more Label Properties
[self.contentView addSubview:self.myLabel];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
// Override run time properties
}
Finally use your custom cell like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"Home-Cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Home-Cell"];
}
cell.myLabel.text = self.data[indexPath.row];
return cell;
}
As a side note, I hope you know that you get textLabel and detailTextLabel free from default UITableViewCell implementation.

iOS 8 Draw outside of UITableViewCell bounds

thats simple I want my UILabel inside UITableViewCell draw outside of cell bounds to overlap tableHeaderView.
my tableViewView has only 1 row and tableHeaderView. I have not subclassed UITableViewCell
what I have done:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.contentView.superview setClipsToBounds:NO];
[cell.contentView setClipsToBounds:NO];
[cell setClipsToBounds:NO];
}
but still cell clips its subview.
whats your idea?
#anhtu thanks for your hint. I just sent tableHeaderView to back which fixed the problem
[self.tableView sendSubviewToBack:self.tableView.tableHeaderView];
if you have only one cell and this label will appear only one time
put label outside the table view and put constrains for it

How to change Custom Cell's UILabel Text?

I have a table view with a custom protoype cell and the cell has 3 different labels on it. How do I get access to these labels? I need to change their texts. I've searched around everywhere and haven't found something that helps me in this case. I have the below code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"basicCell" forIndexPath:indexPath];
//change cell's 3 labels' text here
return cell;
}
How can I access the above cell's 3 different labels that it has?
You need to make a class that is a subclass of UITableViewCell, and set the custom cell's class to the class you made. Then you want to link the labels using IBOutlets in the cell to the subclass you made.
Finally, in -tableView:cellForRowAtIndexPath:, you should cast the cell to your new subclass so you get access to those IBOutlets. Assuming your outlets are named someLabelOutlet1, someLabelOutlet2, and someLabelOutlet3, do the following after you finished subclassing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SomeTableViewCellSubclass *cell = (SomeTableViewCellSubclass *)[tableView dequeueReusableCellWithIdentifier:#"basicCell" forIndexPath:indexPath];
cell.someLabelOutlet1.text = #"sometext"
cell.someLabelOutlet2.text = #"sometext"
cell.someLabelOutlet3.text = #"sometext"
return cell;
}
In cellForRowAtIndexPath, you can access these labels
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:#"CustomCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
// set common properties here
cell.lbl1.textColor=[UIColor whiteColor];
cell.lbl1.backgroundColor=[UIColor redColor];
cell.lbl1.font=[UIFont fontWithName:#"Verdana" size:16.0];
}
cell.lbl1.text=#"lbl1Txt";
cell.lbl2.text=#"lbl2Txt";
cell.lbl3.text=#"lbl3Txt";
return cell;
}

Getting the actual UITableViewCell for a tapped UITableView created using the Storyboard.

I've created a UITableView in my application. I've Created a custom UITableViewCell called VideoCell which contains a UIWebview, a UIImageView and a UILabel.
The UIWebView is the same size as the UIImageView and in the same location, but behind the UIImageView. I want to hide the UILabel and the UIIMageView when I tap the cell, so that the UIWebView is revealed.
The VideoCell has a ReuseIdentifier set in the storyboard editor which I use for dequeing the VideoCell from in tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"VideoCell";
VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
...
return cell;
}
How can I get a reference to the actual cell that I tap in tableView:didSelectRowAtIndexPath: in order to visibly change the properties?
I've tried just dequing with the same arguments
VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Which returns me a VideoCell, but with no fields set up
I've also tried calling tableView:cellForRowAtIndexPath:
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
but that returns me a new instance of the VideoCell with the correct fields set up, and this is not the one that is currently displayed.
VideoCell *cell = (VideoCell*)[tableView cellForRowAtIndexPath:indexPath];
Don't call the data source method present in ViewController, call the method on actual instance of your UITableView
Try using this
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
instead of
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
cellForRowAtIndexPath:
is the correct way to get the instance of the cell. If you are unable to retrieve the exact cell you want in this way, it just means that your implementation in that method is wrong.

Resources