Cannot set text in textview in my custom tableView cell - ios

I am trying to load my UITextView on UItableViewCell with data but unable to set text. UITextViewDelegate is also set and attached to view controller. The text string is not empty as I checked it using debugger.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];
//CommentCell is my custom cell with textView.
CommentCell *commentsCell = (CommentCell*)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CommentCellIdentifier];
}
//Setting the images for all buttons in service row.
[commentsCell.deletecomment setImage:deleteComment forState:UIControlStateNormal];
[commentsCell.editcomment setImage:editComment forState:UIControlStateNormal];
commentsCell.deletecomment.layer.borderColor = [UIColor blackColor].CGColor;
commentsCell.editcomment.layer.borderColor = [UIColor blackColor].CGColor;
NSInteger commentIndex = 2;
//CommentsArray is NSArray with data.
[commentCell.comments setText:[NSString stringWithFormat:#"%#",[[commentsArray objectAtIndex:indexPath.row] objectAtIndex:commentIndex]]];
return cell;
}

Try changing this line:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];
to this line:
CommentCell *cell = (CommentCell*)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];

I was using wrong cellname. Just replaced "commentCell" with "commentsCell" and everything starts working.
[commentsCell.comments setText:[NSString stringWithFormat:#"%#",[[commentsArray objectAtIndex:indexPath.row] objectAtIndex:commentIndex]]];`enter code here`

If you do not need scrolling the textview DISABLE SCROLLING in order for the text to appear.
I am not sure if this is caused by the fact that both the tableview and textview are descendants of UIScrollView, but it could be.
It helped me when I was experiencing the same issue

Related

Bug with UITableViewCell ios

I'm working on an iOS project .I created an UITableView to display a list of drones . Everything works fine but I noticed that there is a bug when I click on the cell to display the details of the drone . As u can see in the images below.
When the tableView get loded
When clicked on cell
and this my code when I select a cell :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *_storyboardName = #"Main";
UIStoryboard *_storyboard = [UIStoryboard storyboardWithName:_storyboardName bundle: nil];
DroneDetailsViewController *detailsVC = [_storyboard instantiateViewControllerWithIdentifier:#"DroneDetailsViewController"];
detailsVC.droneObj = [drones objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailsVC animated:YES];
[detailsVC.navigationController setNavigationBarHidden:NO animated:YES];
}
This my code for cellForRowAtIndexPath :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"drone";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
DroneObject *droneObjDisplay = nil;
droneObjDisplay = [drones objectAtIndex:indexPath.row];
NSData *data = [[NSData alloc]initWithBase64EncodedString:droneObjDisplay.modelMSide options:NSDataBase64DecodingIgnoreUnknownCharacters];
[self initCellWithComponents:cell :data :droneObjDisplay.modelName :droneObjDisplay.modelNumber];
return cell;
}
and this the initCelWithComponents :
- (void)initCellWithComponents :(UITableViewCell *)cell :(NSData *)dataImage :(NSString *)title :(NSString *)subTitle {
UIView *viewCell = [[UIView alloc] initWithFrame:CGRectMake(VIEW_MARGIN, VIEW_MARGIN, widthtScreen-20, VIEW_IMAGE_HEIGHT)];
viewCell.layer.borderColor = [UIColor colorWithHexString:NSLocalizedString(#"border_color", nil)].CGColor;
viewCell.backgroundColor = [UIColor whiteColor];
viewCell.layer.borderWidth = 0.5f;
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(VIEW_MARGIN,VIEW_MARGIN, VIEW_IMAGE_HEIGHT-(2*VIEW_MARGIN),VIEW_IMAGE_HEIGHT-(2*VIEW_MARGIN))];
image.image=[UIImage imageWithData:dataImage];
[viewCell addSubview:image];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+2*VIEW_MARGIN, VIEW_MARGIN, viewCell.frame.size.width-(VIEW_IMAGE_HEIGHT-(4*VIEW_MARGIN)), 21)];
titleLabel.text = title;
titleLabel.textColor = [UIColor blackColor];
[titleLabel setFont:[UIFont systemFontOfSize:15]];
[viewCell addSubview:titleLabel];
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+2*VIEW_MARGIN, 3*VIEW_MARGIN, viewCell.frame.size.width-(VIEW_IMAGE_HEIGHT-(4*VIEW_MARGIN)), 21)];
subTitleLabel.text = subTitle;
subTitleLabel.textColor = [UIColor blackColor];
[subTitleLabel setFont:[UIFont systemFontOfSize:13]];
[viewCell addSubview:subTitleLabel];
cell.contentView.backgroundColor = [UIColor colorWithHexString:NSLocalizedString(#"gray_background", nil)];
[cell.contentView addSubview:viewCell];
}
any ideas how to fix that ?!
Your cell's style is UITableViewCellStyleSubtitle and you also adding title and subtitle label manually also.
If you are using cell's style as UITableViewCellStyleSubtitle then you can directly use
cell.textLabel.text = #"your title label's text";
cell.detailTextLabel.text = #"your detail label's text";
If you will add another label then it will creates duplicate!!!!
Since you are initialising UIView and UILabel in method inside cellForRowAtIndexPath and adding them as subview, these controls are being added to your cell each time cell is reloaded. Below code identifies each cell with a unique identifier and loads the cell only if that particular cell is not available in UITableView.
I have written droneObjDisplay.droneId to uniquely identify cell, you can write anything for droneObjDisplay.__ that is unique identifier of your object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DroneObject *droneObjDisplay = nil;
droneObjDisplay = [drones objectAtIndex:indexPath.row];
NSString *CellIdentifier = [NSString stringWithFormat:#"drone_%d", droneObjDisplay.droneId];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSData *data = [[NSData alloc]initWithBase64EncodedString:droneObjDisplay.modelMSide options:NSDataBase64DecodingIgnoreUnknownCharacters];
[self initCellWithComponents:cell :data :droneObjDisplay.modelName :droneObjDisplay.modelNumber];
}
return cell;
}
Hope it helps.
The TableViewCell could be designed and used by either one of the following ways,
If you are using the storyboard then you can simply design the prototype cell on the TableView on storyboard itself and then giving an identifier to the cell. If you are using this way then add the component on the storyboard only and give tag to each component to access it in the code if you are not subclassing the UITableViewCell, and dequeue the cell as below,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UILabel *labelValue = (UILabel*) [cell viewWithTag:10];
Design the TableViewCell on the storyboard as you did in 1 way and then simply create a subclass of UITableViewCell class and set that class as a Custom Class of the cell by selecting the cell on storyboard, this way you can create the outlet of each component in the cell and you do not need to access the component by the tags. Below is the code to dequeue the cell,
CellSubClassed *cell = (CellSubClassed*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.labelXYZ.text = arrayDataSource[indexPath.row][#"key"];
This is the old way before storyboard was introduced, Create subclass of UITableView cell and create a xib file with the same name you give to UiTableViewCell subclass, create the outlets of component you want to access. Below is the code to dequeue cell,
NSArray *nib;
static NSString *strNibName = #"CustomCell";
nib = [[NSBundle mainBundle] loadNibNamed:strNibName owner:self options:nil];
CustomCell *cell = [nib objectAtIndex:0];
cell.imageView.image = arrayDataSource[indexPath.row];
The last way is using TableView's default cells, just create the tableView on storyboard/xib and dequeue the cell as below,
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"xyz";
The default tableView cell styles are,
UITableViewCellStyleDefault
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
You can read about more on Apple Developer docs here.
So in your case you are mixing the two ways of dequeuing the cell, if you want to add the component manually as you did and you don't want to design the component on the storyboard or Xib then you just create the subclass of UITableView cell and then create the component in the awakeFromNib method and dequeue the cell as explained in 3 way.
Hope it helps you.
Ok finally, after getting your answers guys , I created a Custom Cell a subclass of UITableViewCell and I added all my components there . The main problem was how I will call it without UI (storyboard or xib ) just by code .
So after the creation of the custom cell I added this line to my viewdidload :
[self.tableView registerClass:[CustomCellTableViewCell class] forCellReuseIdentifier:#"CustomCellTableViewCell"];
then in the cellForRowAtIndexPath I just added the custom cell :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DroneObject *droneObjDisplay = nil;
droneObjDisplay = [drones objectAtIndex:indexPath.row];
static NSString *identifier = #"CustomCellTableViewCell";
CustomCellTableViewCell *cell = (CustomCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
NSData *data = [[NSData alloc]initWithBase64EncodedString:droneObjDisplay.modelMSide options:NSDataBase64DecodingIgnoreUnknownCharacters];
cell.image.image = [UIImage imageWithData:data];
cell.titleLabel.text = droneObjDisplay.modelName;
cell.subTitleLabel.text = droneObjDisplay.modelNumber;
return cell;
}

Adding UIDatePicker to cell content view

I'm attempting to put an instance of UIDatePicker in the accessory view of a UITableView cell, and have been following this SO thread as a template. However, it looks as if the picker is being placed above the cell entirely:
Below is the code I'm using to try to add a Date Picker to the accessory view of a UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellNewRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RowCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
switch (indexPath.row) {
case EmployeeOvertimeRow:
cell.textLabel.text = NSLocalizedString(#"Test", #"One");
_datePicker = [[UIDatePicker alloc]init];
_datePicker.datePickerMode = UIDatePickerModeTime;
//OLD: cell.accessoryView = _datePicker;
//POST EDIT
[cell.contentView addSubview:_datePicker];
break;
default:
break;
}
return cell;
}
Does anyone have any guidance on what I'm doing wrong or how to fix this?
The accessoryView of a UITableViewCell is surely not what you think : it's the little view at the right of the cell, typically, it's an arrow, and it's pretty small, not made to be the width of the screen at all. You should try adding your view to the contentView. You will need to set a bigger height for your cell in the heightForRowAtIndexPath method, too.

Weird Custom UITableViewCell behavior

I got a really weird problem with a custom UITableViewCell. Btw, i am using an UIViewController. So, i crafted the cell in Storyboard (like in the image bellow) and i set it's class to my custom UITableViewCell class. Then I created all the IBOutlets and IBActions in the custom cell class.
My cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
PostTableViewCell *cell = (PostTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setCellContentWithPost:[PostsArray objectAtIndex:indexPath.row]];
return cell;
}
My custom UITableViewCell class:
#import "PostTableViewCell.h"
#implementation PostTableViewCell
- (void)setCellContentWithPost:(SDPost*)post {
self.alpha = 0.f;
self.postTitleLabel.text = post.title;
[self.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:post.thumbnailURL] placeholderImage:nil options:SDWebImageHandleCookies];
[UIView animateWithDuration:0.35 animations:^{
self.alpha = 1.0f;
}];
}
-(void)awakeFromNib{
self.postTitleLabel.textColor = [UIColor whiteColor];
self.postTitleLabel.font = [UIFont fontWithName:#"Montserrat-Regular" size:16.5];
self.readingTimeView.backgroundColor = [UIColor colorWithRed:0.33 green:0.74 blue:0.15 alpha:1];
self.readingTimeView.layer.cornerRadius = 4;
self.readingTimeLabel.textColor = [UIColor whiteColor];
self.readingTimeLabel.font = [UIFont fontWithName:#"Montserrat-Bold" size:11.75];
self.commentsCountView.backgroundColor = [UIColor colorWithRed:0.74 green:0.19 blue:0.4 alpha:1];
self.commentsCountView.layer.cornerRadius = 4;
self.commentsCountLabel.textColor = [UIColor whiteColor];
self.commentsCountLabel.font = [UIFont fontWithName:#"Montserrat-Bold" size:11.75];
}
I tried to style the cell from the initWithStyle method of the UITableViewCell, but for some reason it never gets called, so i ended up doing this in awakeFromNib.
So, the problem is: I think i am doing something wrong, because as you can see in this GIF (https://dl.dropboxusercontent.com/s/6crcjbmitr5fmk7/Untitled%20%281%29.gif?m=), the heart button it get's automatically turned on/off as i scroll through the cells.
Can anyone of you guys help me fix this ? Thanks a lot!
Each time the cell is displayed it takes value from your PostsArray array. So when you click on the heart, you should update its corresponding object in the array.
That happens because you have the same CellIdentifier for all your cells, which is fine in this case if you are very careful on how you are handling the "heart" element.
You should have a way to determine which elements on the cell have been "liked". As I suppose you need to know on which elements your user pressed the heart button.
When initialising/reusing your cell you need to be sure that the heart button is set properly to "red/on" or "white/off".
For instance :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
PostTableViewCell *cell = (PostTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setHeartState:[[AnArray objectAtIndex:indexPath.row] hasBeenLikedByUser]]
[cell setCellContentWithPost:[PostsArray objectAtIndex:indexPath.row]];
return cell;
}
It is just a quick draft.
Hope that helps
I managed to solve this by checking if the hearth should be filled or not. I stored the post id using NSUserDefaults and in setCellContentWithPost i added an if statement that checks if the cell's post is favorited.
Thanks for interest guys!

Need to remove border between cells in UITableView when using background image in iOS

I am having trouble removing the white border that appears between each cell inside my UITableView. I have tried using:
[myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
inside my viewDidLoad method, but unfortunately that does not seem to work. I am using a background image for my cells to display, and it is the border between these images that I wish to remove. The image itself is the size of each cell, and the size of each cell I set in my CustomTableViewCell class as follows:
self.contentView.frame = CGRectMake(0, 0, 320, 80);
My cellForRowAtIndexPath method is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"mycell";
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.ID.text = [[DataModel sharedInstance].ID objectAtIndex:indexPath.row];
cell.Name.text = [[DataModel sharedInstance].Name objectAtIndex:indexPath.row];
cell.Date.text = [[DataModel sharedInstance].Date objectAtIndex:indexPath.row];
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cell.png"]]];
return cell;
}
Despite everything I have done, I am seeing a clear white border between my cells in the table. I really would like to remove this. Can anyone see what I am doing wrong?
Inside Interface Builder - change the separator to None and then the separator color to clear color:
Setting UITableView separatorColor at viewDidLoad might be too early. Try viewWillAppear instead.

How can I stop my table view cells loading their subviews multiple of times in iOS?

I am using FontLabel in the view cells of the table.
- (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];
}
FontLabel *author_name = [[FontLabel alloc] initWithFrame:CGRectMake(58, 10, 217, 16) fontName:#"MyriadPro-Bold" pointSize:12.0f];
author_name.numberOfLines = 1;
author_name.text = [dummyArray objectAtIndex:indexPath.row];
author_name.textColor = [UIColor colorWithRed:0.580 green:0.776 blue:0.329 alpha:1.000];
author_name.backgroundColor = [UIColor clearColor];
[cell addSubview:author_name];
return cell;
}
But the label is loaded multiple times. I can see that it is getting bolder and bolder. How can I fix it?
When you call addSubview, it's not removing the old view. remember that cells are reused so the view you added in the last iteration is still there.
You should instead subclass UITableViewCell so that you can add a UILabel to it.
Edit:
Or you can do
author_name.tag = 10;
and then
FontLabel *author_name = (FontLabel *)[cell.contentView viewWithTag:10];
when you want to retrieve it.

Resources