UITextView in a grouped UITableView's cell - objective-c

Please, explain me: how to programmatically create an UITextView in UITableViewCell (UITableView has a grouped style). The text view size should be equal to size of the cell.
The UITextView has a gap between its text and its border (top left corner), so I need the correct coordinates to properly place the text view on a cell.
UPDATE: I've solved my question. See my self-answer below. Thanks!

It was simple:
textView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);

Here you go:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Prototype Cell"];
UITextField *textfield;
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"Prototype Cell"];
textfield = [[UITextField alloc] init];
[cell.contentView addSubview:textfield];
textfield.tag = TEXT_FIELD_TAG; //Suppose you defined TEXT_FIELD_TAG someplace else
}
else
{
textfield = [self.view viewWithTag: TEXT_FIELD_TAG];
textfield.frame = cell.frame;
}
return cell;
}

Set "contentInset" of UITextView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *sCellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDetail
reuseIdentifier:sCellIdentifier];
}
UITextView *textView = [UITextView alloc]init];
textView.contentInset = UIEdgeInsetsMake(-8,-8,-8,-8); //Change according ur requirement
textView.frame = cell.frame;
[textView setUserInteractionEnabled:FALSE];
[textView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubView:textView];
return cell;
}

Related

UITableView willDisplayCell Acting Weird, Cells borders adjusted only after reuse

I am trying to have 10 pixels gap between cells and the edge of the screen. I implemented willDisplayCell:forRowAtIndexPath and also the heightForCell since am using two different prototype cells in my table view.
The problem am having is really weird. The cells are only adjusted when i pull the list up, am guessing only when they are reused! check the images please.![enter image description here][1]
This is the initial screen:
https://www.dropbox.com/s/epl2n8a5zohhdmz/Edited.screenShot.01.png?dl=0
This is the screen after the table view is scrolled:
https://www.dropbox.com/s/c81dx9wmaq0mgr5/Edited.screenShot.02.png?dl=0
Here is my code:
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.layer.borderWidth = 5.0f;
cell.layer.borderColor = [UIColor grayColor].CGColor;
cell.layer.cornerRadius = 7.0;
cell.layer.bounds = CGRectMake(0, 0, 260, cell.bounds.size.height - 10);
cell.backgroundColor = [UIColor colorWithRed:207/255.0 green:207/255.0 blue:207/255.0 alpha:1.0];
CGRect cellFrame = CGRectMake(20, cell.frame.origin.y, 280, cell.frame.size.height);
cell.frame = cellFrame;
cell.contentView.frame = cellFrame;
[cell.contentView alignmentRectForFrame:cellFrame];
[cell setNeedsLayout];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[self.namesList[indexPath.row] objectForKey:#"Relation"] length] == 0) {
return 69.0;
} else {
return 89.0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableString *details = [[NSMutableString alloc] init];
UITableViewCell *cell = [[UITableViewCell alloc] init];
UILabel *nameLabel;
UILabel *relationLabel;
UILabel *ageLabel;
UILabel *dayLabel;
UILabel *dateLabel;
Helper *dateConvert = [[Helper alloc] init];
if ([[self.namesList[indexPath.row] objectForKey:#"Relation"] length] == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell2" forIndexPath:indexPath];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell3" forIndexPath:indexPath];
relationLabel = (UILabel *) [cell viewWithTag:2];
relationLabel.text = [self.namesList[indexPath.row] objectForKey:#"Relation"];
}
nameLabel = (UILabel *) [cell viewWithTag:1];
ageLabel = (UILabel *) [cell viewWithTag:3];
dayLabel = (UILabel *) [cell viewWithTag:4];
dateLabel = (UILabel *) [cell viewWithTag:5];
[details appendString:[self.namesList[indexPath.row] objectForKey:#"Age"]];
[details appendString:#")"];
ageLabel.text = details;
dayLabel.text = [dateConvert getWeekDay:[self.namesList[indexPath.row] objectForKey:#"Date"]];
dateLabel.text = [self.namesList[indexPath.row] objectForKey:#"Date"];
return cell;
}
I have spent days trying to find the problem :( every help is greatly appreciated :)
Why not make your life easier and design the cell in Interface Builder to have the 10 px space already. You can add a UIView, make that 10 px from top, left, right and bottom, then put you UI elements inside of that. I would suggest subclassing your UITableViewCells and have code in those to display your data and set the borders.

How to set separator of one section of tableView to clear

I have four sections in my tableView, and I want only the last section to have a clear separator. I know how to do this for the whole tableView, but cannot find any solution for my question. The only solution that I have thought of is to create the cell as an image in Photoshop and set that image as the cell background. Does anyone have an idea of how to do this without having to use Photoshop?
Try this one
In View didLoad
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
And
-(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];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 1)];
view.backgroundColor = [UIColor greenColor];
view.tag = 101;
[cell addSubview:view];
}
if (indexPath.section == 0)
{
[[cell viewWithTag:101] setHidden:YES];
}
else if(indexPath.section == 1)
{
[[cell viewWithTag:101] setHidden:NO];
}
return cell;
}

Hide first Cell of UITableView on iPad

I have a TableView with various entries. Sometimes i need to scroll down, sometimes there are only a few entries, so everything is visible.
I want some settings and filters in my First Cell, but it should not be visible, only when the user scrolls down.
My Trys:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
float rowHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];
self.tableView.contentOffset = CGPointMake(0, rowHeight);
}
and
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTopanimated:NO];
}
Both did not work.
Any advice on my problem?
Are you looking for something like this?
[self.tableView setContentInset:UIEdgeInsetsMake(t,l,b,r)];
this will shift your tableView whatever way you want, but you can definitely scroll back.
Try your logic in the TableView Delegate methods
- (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] autorelease];
}
if (indexPath.row == 0) {
//Your filters here
}
else {
//display the cell
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Also if you dont wish the first cell to be displayed just write
return nil; //for condition of indexpath.row == 0
And also adjust the height for the row in
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0 && isDisplayThisSection) {//first visible
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 25)] autorelease];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(17, 10, 300, 25)];
[titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
titleLabel.text = #"Temp Test";
[titleLabel setBackgroundColor:[UIColor clearColor]];
[headerView addSubview:titleLabel];
[titleLabel release];
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}
set value to 0
Hope this help!!
Try your logic in the TableView Delegate methods
- (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] autorelease];
}
if (indexPath.row == 0) {
//Your filters here
}
else {
//display the cell
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Also if you dont wish the first cell to be displayed just write
return ; //for condition of indexpath.row == 0
And also adjust the height for the row in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
set value to 0
And if you wish to implement like Pull_to_Refresh, you would better write your logic in
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
Hope this help!!

Adding a UIPickerView to a UITableView cell

I am trying to add a pickerView to a row in my table view, but after doing this, all I see is a black rectangle inside the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
pickerView = [[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200, 300)] autorelease];
[cell.contentView addSubview:pickerView];
}
}
I really don't have a clue how to solve this.
Can anyone help me, please?
May be this can help: DateCell with TableViewController

where can find a example on adding UIButton in each UITableCell

Where can find a example on adding UIButton in each UITableCell?
You need to create a UIbutton and jus addSubView to the cell it will work fine.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UIImageView * accerTypeImage;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *button = [[UIButton allo] init];
button.frame = //give the frame in UITableViewCell.
button.tag = indexPath.row;
[cell addSubView:button];
[button release];
return cell;
}