I need to set the position of my map view in a table view cell. I simply set the frame in cellForRow method.
some of the map views are misplaced. When I scroll up the table and scroll down to let it reappear (to reuse the table cell), then it is fixed.
Note
the y position is wrong but the x position is correct.
I use the same way (simply set frame) to set the pictures position and they are always correct. So the problem is map view itself instead of how to position the frame.
The following screenshot shows 3 map views, the middle one has a wrong y position
EDIT:
The UI part is quite complicated. inside cellForRowAtIndexPath method, I dequeue a MessageCell and call its setupWithMessage:(Message *)message method and then I check if the message is of type location: (a location type message has an optional text view and a map view)
self.textView.hidden = NO;
self.mapView.hidden = NO;
self.textView.text = message.text;
[Helper setupMapView:self.mapView posx:message.posx posy:message.posy];
CGSize size1 = [UIHelper sizeWithText:message.text entity:message];
CGSize size2 = [UIHelper sizeWithMapEntity:message];
CGRect frame = [UIHelper adjustTextView:self.textView textViewSize:size1 extraView:self.mapView extraViewSize:size2 entity:message];
+ (CGRect)adjustTextView:(UITextView *)textView textViewSize:(CGSize)textViewSize extraView:(UIView *)extraView extraViewSize:(CGSize)extraViewSize entity:(id)entity {
if (textView == nil || extraView == nil || CGSizeEqualToSize(textViewSize, CGSizeZero) || CGSizeEqualToSize(extraViewSize, CGSizeZero)) {
CGSize targetSize;
UIView *targetView;
if (textView == nil || CGSizeEqualToSize(textViewSize, CGSizeZero)) {
targetView = extraView;
targetSize = extraViewSize;
}
else {
targetView = textView;
targetSize = textViewSize;
}
return [self adjustContentView:targetView size:targetSize entity:entity];
}
else {
CGRect frame1 = [self adjustContentView:textView size:textViewSize entity:entity];
CGRect frame2 = [self adjustContentView:extraView size:extraViewSize entity:entity];
frame2.origin.y += frame1.size.height + OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW;
extraView.frame = frame2;
return CGRectMake(frame1.origin.x, frame1.origin.y, MAX(frame1.size.width, frame2.size.width), frame1.size.height + frame2.size.height + OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW);
}
Note that the "extraView" above can refer to any view below a text view, such as an UIImageView
+ (CGRect)adjustContentView:(UIView *)contentView size:(CGSize) size entity:(id)entity {
float photoSideOffset = 60;
float topOffset = 30;
CGRect frame;
frame.origin.y = topOffset;
frame.size = size;
if ([DBHelper isMyEntity:entity]) {
frame.origin.x = 320 - size.width - photoSideOffset;
}
else {
frame.origin.x = photoSideOffset;
}
contentView.frame = frame;
if ([contentView isKindOfClass:[UITextView class]]) {
[(UITextView *)contentView sizeToFit];
}
return contentView.frame;
}
EDIT 2: i use the same UIHelper methods for photo messages and the coordinates are correct:
EDIT 3: the value of size1 size2 and frame
Approach 1: alloc init UITableViewCell inside your cellForRowAtIndexPath
Simple solution is to cell = [[UITableViewCell alloc]init]; alloc init every time
(it will make new cell each time instead caching/or reusing )
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
/*
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
*/
// Try below code
cell = [[UITableViewCell alloc]init];
------
-------
return cell;
}
Approach 2: removeFromSuperView
for (UIView *subview in self.view.subviews) {
if ([subview isKindOfClass:[UIButton class]]) { // set kind as per your own requirement
[subview removeFromSuperview];
}
}
Sample of code
MultiMap.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MultiMap : UIViewController
<UITableViewDelegate,UITableViewDataSource,MKMapViewDelegate>
{
MKMapView *mapView;
}
#property (retain, nonatomic) IBOutlet UITableView *mainTableView;
#end
MultiMap.m
#import "MultiMap.h"
#interface MultiMap ()
#end
#implementation MultiMap
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
self.title=#"Multi Map";
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"multi";
UITableViewCell *cell=nil;
cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
mapView=[[MKMapView alloc]initWithFrame:CGRectMake(25, 25, 275, 150)];
mapView.delegate=self;
[mapView setBackgroundColor:[UIColor whiteColor]];
[mapView setTintColor:[UIColor whiteColor]];
[cell addSubview:mapView];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#""];
annotationView.canShowCallout = YES;
return annotationView;
}
- (void)dealloc {
[_mainTableView release];
[super dealloc];
}
#end
May you get help..
Please make sure your textView frame is 0. for the case the mapView doesn't show at accurate place additionally you can try following.
+ (CGRect)adjustTextView:(UITextView *)textView textViewSize:(CGSize)textViewSize extraView:(UIView *)extraView extraViewSize:(CGSize)extraViewSize entity:(id)entity {
if (textView == nil || extraView == nil || CGSizeEqualToSize(textViewSize, CGSizeZero) || CGSizeEqualToSize(extraViewSize, CGSizeZero)) {
CGSize targetSize;
UIView *targetView;
if (textView == nil || CGSizeEqualToSize(textViewSize, CGSizeZero)) {
targetView = extraView;
targetSize = extraViewSize;
}
else {
targetView = textView;
targetSize = textViewSize;
}
return [self adjustContentView:targetView size:targetSize entity:entity];
}
else {
CGRect frame1 = [self adjustContentView:textView size:textViewSize entity:entity];
CGRect frame2 = [self adjustContentView:extraView size:extraViewSize entity:entity];
frame2.origin.y += frame1.size.height + OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW;
extraView.frame = frame2;
[[extraView superview] setNeedsLayout]; //Addition
return CGRectMake(frame1.origin.x, frame1.origin.y, MAX(frame1.size.width, frame2.size.width), frame1.size.height + frame2.size.height + OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW);
}
UPDATE: Or you can update the following
+ (CGRect)adjustContentView:(UIView *)contentView size:(CGSize) size entity:(id)entity {
float photoSideOffset = 60;
float topOffset = 30;
CGRect frame;
frame.size = size;
if ([DBHelper isMyEntity:entity]) {
frame.origin.x = 320 - size.width - photoSideOffset;
}
else
{
frame.origin.x = photoSideOffset;
}
for (id view in [[contentView superview] subviews])
{
if (view != contentView)
{
if ([view respondsToSelector:#selector(frame)])
{
topOffset = (([view frame].origin.y + [view frame].size.height)<topOffset)?([view frame].origin.y + [view frame].size.height):topOffset;
}
}
}
if ((int)topOffset != 30)
{
topOffset += OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW;
}
frame.origin.y = topOffset;
contentView.frame = frame;
if ([contentView isKindOfClass:[UITextView class]]) {
[(UITextView *)contentView sizeToFit];
}
return contentView.frame;
}
Also make sure you update the height of the cell accordingly using - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Related
I have a UITableView with a custom cell and at the beginning all is empty.
My UITableViewCell has a UITextView and when i tap on the screen i create a new cell and fire on it:
#import "MemoViewController.h"
#import "MemoViewCell.h"
#import "MemoModel.h"
static NSString *CellIdentifier = #"MemoCell";
#interface MemoViewController () <UITextViewDelegate>
#property (nonatomic, strong) MemoModel *model;
#property (nonatomic, strong) NSMutableDictionary *offscreenCells;
#end
#implementation MemoViewController
{
NSIndexPath *currentIndexPath;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.model = [[MemoModel alloc] init];
//[self.model populateDataSource];
self.offscreenCells = [NSMutableDictionary dictionary];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[MemoViewCell class] forCellReuseIdentifier:CellIdentifier];
self.tableView.rowHeight = UITableViewAutomaticDimension;
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(insertNewRow:)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:gestureRecognizer];
currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:#"Modifica"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(editButtonPressed:)];
self.navigationItem.rightBarButtonItem = editButton;
}
-(IBAction)editButtonPressed:(UIBarButtonItem *)sender
{
if ([sender.title isEqualToString:#"Fine"]) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.model.dataSource count] - 1 inSection:0];
MemoViewCell *cell = (MemoViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.bodyLabel resignFirstResponder];
}
}
-(void)insertNewRow:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
if (indexPath == nil) {
[self.model addObject];
[self.tableView reloadData];
indexPath = [NSIndexPath indexPathForRow:[self.model rowsCount] - 1 inSection:0];
}
[self.navigationItem.rightBarButtonItem setTitle:#"Fine"];
MemoViewCell *cell = (MemoViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.bodyLabel becomeFirstResponder];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(contentSizeCategoryChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)contentSizeCategoryChanged:(NSNotification *)notification
{
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.model.dataSource count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MemoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([self.model.dataSource count] > 0) {
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
cell.bodyLabel.text = [dataSourceItem valueForKey:#"body"];
}
cell.bodyLabel.delegate = self;
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuserIdentifier = CellIdentifier;
if ([self.model.dataSource count] > 0) {
MemoViewCell *cell = [self.offscreenCells objectForKey:reuserIdentifier];
if (!cell) {
cell = [[MemoViewCell alloc] init];
[self.offscreenCells setObject:cell forKeyedSubscript:reuserIdentifier];
}
[cell updateFonts];
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
cell.bodyLabel.text = [dataSourceItem valueForKey:#"body"];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
height += 1;
CGFloat toReturn = height + [self measureHeightOfUITextView:cell.bodyLabel] - 30;
NSLog(#"Height for Row %li is %f", (long)indexPath.row, toReturn);
return toReturn;
} else {
return 44.0;
}
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0;
}
/*
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MemoViewCell *cell = (MemoViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.bodyLabel.userInteractionEnabled = YES;
[cell.bodyLabel becomeFirstResponder];
}
*/
#pragma mark - Text View Delegate
- (void)textViewDidBeginEditing:(UITextView*)textView
{
MemoViewCell* cell = (MemoViewCell *)[self parentCellFor:textView];
if (cell)
{
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
currentIndexPath = indexPath;
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
}
/*
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
MemoViewCell *cell = (MemoViewCell *)[self.tableView cellForRowAtIndexPath:currentIndexPath];
[cell.bodyLabel becomeFirstResponder];
[self.tableView selectRowAtIndexPath:currentIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
return YES;
}
*/
- (UITableViewCell*)parentCellFor:(UIView*)view
{
if (!view)
return nil;
if ([view isMemberOfClass:[MemoViewCell class]])
return (UITableViewCell*)view;
return [self parentCellFor:view.superview];
}
-(void)textViewDidChange:(UITextView *)textView
{
NSMutableDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:currentIndexPath.row];
[dataSourceItem setObject:textView.text forKey:#"body"];
[self.model.dataSource replaceObjectAtIndex:currentIndexPath.row withObject:dataSourceItem];
if ([textView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height != textView.frame.size.height) {
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
- (CGFloat)measureHeightOfUITextView:(UITextView *)textView
{
if ([textView respondsToSelector:#selector(snapshotViewAfterScreenUpdates:)])
{
CGRect frame = textView.bounds;
UIEdgeInsets textContainerInsets = textView.textContainerInset;
UIEdgeInsets contentInsets = textView.contentInset;
CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + textView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;
CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;
frame.size.width -= leftRightPadding;
frame.size.height -= topBottomPadding;
NSString *textToMeasure = textView.text;
if ([textToMeasure hasSuffix:#"\n"])
{
textToMeasure = [NSString stringWithFormat:#"%#-", textView.text];
}
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributes = #{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle };
CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGFloat measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);
return measuredHeight;
}
else
{
return textView.contentSize.height;
}
}
#end
With this code (insertNewRow:) i first create a new object on dataModel, i reload the data to setup all the table and next i make the first responder my last added UITextView.
On textViewDidChange: delegate i update the table to expand the row(s) to the UITextViewContent.
All works fine if i have at least 2 rows. If i have only one row, all rows assume all the same width.
If your cell's height is fixed then just use this method with return.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
but if your cell's height is dynamic then you need to do some calculation and then return the correct height for the cell.
I have a some collectionViews and one table view that are in the same view controller. The rather strange problem is that when i scroll up-down I alway get memory increases.
Instruments show a lot of allocation of VM:CoreAnimation objects but I can't track them down (they are inside collectionView itself).
prepare for reuse is getting called inside the cells, I checked this.
Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.receivedChannels = NO;
self.ItemsDict = [NSMutableDictionary new];
self.crtBatchSet = [NSMutableIndexSet new];
if ([[PSDeviceInfo sharedInstance] is_iPad]) {
self.epgWidth = EPG_WIDTH_IPAD;
} else {
self.epgWidth = EPG_WIDTH_IPHONE;
}
crtBatchSetSize = 0;
self.currentSelecteIndexOfDateCell = 0;
//size
self.widthDictionary = [NSMutableDictionary new];
self.centerXDictionary = [NSMutableDictionary new];
self.layout = [[MultipleLineLayout alloc] initWithWidthDictionary:self.widthDictionary andCenterXDictionary:self.centerXDictionary];
self.ItemsCollectionVIew.collectionViewLayout = self.layout;
self.ItemsCollectionVIew.showsHorizontalScrollIndicator = NO;
self.ItemsCollectionVIew.showsVerticalScrollIndicator = NO;
self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
[self.ItemsCollectionVIew registerClass:[ItemColectionCell class] forCellWithReuseIdentifier:#"ItemColectionCellID"];
[self.hoursCollectionView registerClass:[HoursCollectionViewCell class] forCellWithReuseIdentifier:#"HoursColectionCellID"];
[self.datePickerCollectionView registerClass:[DatePickerCollectionViewCell class] forCellWithReuseIdentifier:#"DatePickerColectionCellID"];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == self.ItemsCollectionVIew){
self.channelsTableView.contentOffset = CGPointMake(0, self.ItemsCollectionVIew.contentOffset.y);
self.hoursCollectionView.contentOffset = CGPointMake(self.ItemsCollectionVIew.contentOffset.x, 0);
}
if (scrollView == self.channelsTableView) {
self.ItemsCollectionVIew.contentOffset = CGPointMake(self.ItemsCollectionVIew.contentOffset.x,self.channelsTableView.contentOffset.y);
}
if (scrollView == self.hoursCollectionView) {
self.ItemsCollectionVIew.contentOffset = CGPointMake(self.hoursCollectionView.contentOffset.x,self.ItemsCollectionVIew.contentOffset.y);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *channelCellID = #"ChannelTableCellID";
ChannelTableCell *cell = [tableView dequeueReusableCellWithIdentifier:channelCellID forIndexPath:indexPath];
cell.myIndexInTable = indexPath.row;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell getData];
return cell;
}
#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.ItemsCollectionVIew reloadData];
}
#pragma mark - UICollectionViewDataSource Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
if (collectionView == self.datePickerCollectionView || collectionView == self.hoursCollectionView) {
return 1;
}else{
if (self.receivedChannels) {
return totalNoChannels;
} else {
return 1;
}
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (collectionView == self.datePickerCollectionView) {
return DELTA_DAYS + 1;
}else if (collectionView == self.hoursCollectionView) {
return 48; //24 hours * 2
}else{
// if (collectionView == self.ItemsCollectionVIew){
NSArray *sectionItems = self.ItemsDict[#(section)];
if (sectionItems) {
// return sectionItems.count;
return 100;
} else {
return 100;
}
}
return 1;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView == self.datePickerCollectionView) {
static NSString *ItemCellID = #"DatePickerColectionCellID";
DatePickerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];
[self setDayAndDateforCell:cell at:indexPath];
[cell setBackgroundColor:[self getColorForCell:self.currentSelecteIndexOfDateCell == indexPath.row ? YES:NO]];
return cell;
}else if (collectionView == self.hoursCollectionView){
static NSString *ItemCellID = #"HoursColectionCellID";
HoursCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];
cell.hourLabel.text = [self getTimeLineCellValuerFor:indexPath];
return cell;
} else{
// if (collectionView == self.datePickerCollectionView) {
static NSString *ItemCellID = #"ItemColectionCellID";
ItemColectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];
NSArray *Items = self.ItemsDict[#(indexPath.section)];
if (indexPath.row >= Items.count) {
return cell;
}
MvpItem *prog = Items[indexPath.row];
[cell updateInfo:prog];
[cell setBackgroundColor:[self getColorForCell:NO]];
return cell;
}
}
#pragma mark - UICollectionViewDelegate Methods
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView == self.datePickerCollectionView) {
self.currentSelecteIndexOfDateCell = indexPath.row;
}
if (collectionView == self.ItemsCollectionVIew) {
[self pushDetailsatIndexPath:indexPath];
}
}
The code for the tableView cell is:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setBorderWidth:1.0f];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)prepareForReuse{
self.thumbnailImage.image = nil;
self.channel = nil;
[self clearDelegate];
[super prepareForReuse];
}
- (void)dealloc
{
[self clearDelegate];
}
- (void)clearDelegate
{
NSString *url = self.channel.media_content.thumbPoster.url;
// [[ImageManager sharedInstance] removeDelegate:self forImgUrl:url];
[[TVManager sharedInstance] removeDelegate:self];
}
- (void)getData
{
[[TVManager sharedInstance] getChannelAndItemssForIndex:self.myIndexInTable forDateDelta:0 forDelegate:self];
}
- (void)didReceiveTotalNoChn:(NSInteger)totalChnNo{
return;
}
- (void)didReceiveChannel:(MvpChannel *)channel withItemss:(NSArray *)Itemss forDateDelta:(NSInteger)date withIndex:(NSUInteger)index{
if (index != self.myIndexInTable) {
return;
}
if ([self.channel isEqual:channel]) {
return;
}
self.channel = channel;
NSString *imgUrl = self.channel.media_content.thumbPoster.url;
// UIImage *img = [[ImageManager sharedInstance] getImageForUrl:imgUrl forIndex:self.myIndexInTable withDelegate:self];
// if (img) {
// self.thumbnailImage.image = img;
// }
}
#pragma mark - Image Delegate
- (void)didReceiveImage:(UIImage *)image forIndex:(NSInteger)index
{
if (index != self.myIndexInTable) {
return;
}
if (image) {
self.thumbnailImage.contentMode = UIViewContentModeScaleAspectFit;
self.thumbnailImage.image = image;
[self setNeedsLayout];
}
}
And inside the collectionViewCell:
#interface ItemColectionCell ()
#property (retain, nonatomic) UILabel *titleLabel;
#property (retain, nonatomic) UILabel *genreLabel;
#property (retain, nonatomic) UILabel *intervalLabel;
#end
#implementation ItemColectionCell //collection
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setBorderWidth:1.0f];
self.titleLabel = [[UILabel alloc] init];
self.genreLabel = [[UILabel alloc] init];
self.intervalLabel = [[UILabel alloc] init];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.genreLabel setBackgroundColor:[UIColor clearColor]];
[self.intervalLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setTextColor:[UIColor whiteColor]];
[self.genreLabel setTextColor:[UIColor whiteColor]];
[self.intervalLabel setTextColor:[UIColor whiteColor]];
[self.titleLabel setFont:[UIFont fontWithName:#“cf-Bold" size:15]];;
[self.genreLabel setFont:[UIFont fontWithName:#“cf-Regular" size:11]];;
[self.intervalLabel setFont:[UIFont fontWithName:#“cf-Regular" size:11]];;
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void)prepareForReuse{
self.titleLabel.text = nil;
self.genreLabel.text = nil;
self.intervalLabel.text = nil;
[self.titleLabel removeFromSuperview];
[self.genreLabel removeFromSuperview];
[self.intervalLabel removeFromSuperview];
[super prepareForReuse];
}
- (void)updateInfo:(MvpItem*)item{
if (!item) {
return;
}
NSDateFormatter *startTimeFormat = [[NSDateFormatter alloc] init];
[startTimeFormat setDateFormat:#"hh:mm"];
NSDateFormatter *endTimeFormat = [[NSDateFormatter alloc] init];
[endTimeFormat setDateFormat:#"hh:mm a"];
NSString *startTime = [startTimeFormat stringFromDate:item.startTime];
NSString *endTime = [endTimeFormat stringFromDate:item.endTime];
self.titleLabel.frame = CGRectMake(10, 7, self.frame.size.width - 10, 15);
self.genreLabel.frame = CGRectMake(10, 20, self.frame.size.width - 10, 15);
self.intervalLabel.frame = CGRectMake(10, 32, self.frame.size.width - 10, 15);
self.titleLabel.text = item.title;
self.genreLabel.text = #“tewst”;
self.intervalLabel.text = [NSString stringWithFormat:#"%# - %#", startTime, endTime];
[self addSubview:self.titleLabel];
[self addSubview:self.genreLabel];
[self addSubview:self.intervalLabel];
}
What ca I do to solve this problem?
Did you try to remove the cell or heavy tasks triggered by displaying that cell when it did end displaying?
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
or
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
i'm using a tableview to load datas from my college db, the table load the tablecell normally... but when i scroll down the table the name of the discipline goes well but the grade is showing up one on top of above
why is that?
#import "NFMainViewController.h"
#import "NFData.h"
#interface NFMainViewController ()
#end
#implementation NFMainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
data = [[NFData getData] objectForKey:#"data"];
cursoData = nil;
cursosView = [[UIViewController alloc] init];
[cursosView setTitle:#"Cursos"];
cursosTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[cursosTable setDelegate:self];
[cursosTable setDataSource:self];
[cursosView.view addSubview:cursosTable];
[self pushViewController:cursosView animated:NO];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - TableView delegates
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == cursosTable) {
return [data count];
} else {
return [cursoData count];
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *ident = #"headerIdent";
UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ident];
if (view == nil) {
view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:ident];
}
if (tableView == cursosTable) {
view.textLabel.text = [[data objectAtIndex:section] objectForKey:#"unidade"];
} else {
NSDictionary *temp = [cursoData objectAtIndex:section];
view.textLabel.text = [NSString stringWithFormat:#"%#º/%#", [temp objectForKey:#"semestre"], [temp objectForKey:#"ano"]];
}
return view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == cursosTable) {
return [[[data objectAtIndex:section] objectForKey:#"cursos"] count];
} else {
return [[[cursoData objectAtIndex:section] objectForKey:#"disciplinas"] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ident = #"cellIdent";
UITableViewCell *view = [tableView dequeueReusableCellWithIdentifier:ident];
if (view == nil) {
view = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
}
if (tableView == cursosTable) {
view.textLabel.text = [[[[data objectAtIndex:indexPath.section] objectForKey:#"cursos"] objectAtIndex:indexPath.row] objectForKey:#"curso"];
} else {
UIFont *font = [UIFont fontWithName:#"Arial" size:10.0f];
view.textLabel.font = font;
view.selectionStyle = UITableViewCellSelectionStyleNone;
NSDictionary *temp = [[[cursoData objectAtIndex:indexPath.section] objectForKey:#"disciplinas"] objectAtIndex:indexPath.row];
view.textLabel.text = [temp objectForKey:#"disciplina"];
CGRect notaRect = view.bounds;
notaRect.origin.x = notaRect.size.width - 70.0f;
notaRect.size.width = 50.0f;
UILabel *nota = [[UILabel alloc] initWithFrame:notaRect];
nota.textAlignment = NSTextAlignmentRight;
nota.font = font;
nota.text = [temp objectForKey:#"nota"];
[view addSubview:nota];
CGRect labelRect = view.textLabel.frame;
labelRect.size.height -= 60;
view.textLabel.frame = labelRect;
CGRect progRect = view.bounds;
progRect.origin.x += 6.0f;
progRect.size.width -= 12.0f;
progRect.origin.y += progRect.size.height - 6.0f;
progRect.size.height = 5.0f;
UIProgressView *prog = [[UIProgressView alloc] initWithFrame:progRect];
int faltas = [[temp objectForKey:#"faltas"] intValue];
int maximo = [[temp objectForKey:#"maximo"] intValue];
float value = 1.0f * faltas / maximo;
if (value > 1.0f) {
prog.progressTintColor = [UIColor blackColor];
} else if (value == 1.0f) {
prog.progressTintColor = [UIColor redColor];
} else if (value >= 0.7f) {
prog.progressTintColor = [UIColor yellowColor];
}
[prog setProgress:value];
[view addSubview:prog];
}
return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == cursosTable) {
cursoData = [[[[data objectAtIndex:indexPath.section] objectForKey:#"cursos"] objectAtIndex:indexPath.row] objectForKey:#"epocas"];
notasView = [[UIViewController alloc] init];
[notasView setTitle:#"Disciplinas"];
notasTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[notasTable setDelegate:self];
[notasTable setDataSource:self];
[notasView.view addSubview:notasTable];
[self pushViewController:notasView animated:YES];
}
}
#end
Your nota UILabel is created each time a UITableViewCell is dequeued. So the first time the tableview loads everything is fine. Then when you start scrolling, your code reuse cells with the nota label already created, but you add another label on top of it. You need to reuse the label previously created.
The best way is to create a UITableViewCell subclass with a nota property for instance.
I am trying to dynamically resize some UITableViewCell's based on the height of UITextView's contained within them.
There's loads of solutions to this by keeping a pointer to the UITextView and getting it's content size in heightForRowAtIndexPath however when the whole table is created dynamically with an unknown number of rows and an unknown number of them rows contain UITextView's this just isn't possible.
It would be easy if I could call the cell in question during heightForRowAtIndexPath but that causes an infinite loop and crash as this method is called before any cell's are even created.
Any other solutions?
I am using a UITableViewCell subclass for my cell like this:
- (void)initalizeInputView {
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.textView = [[UITextView alloc] initWithFrame:CGRectZero];
self.textView.autocorrectionType = UITextAutocorrectionTypeDefault;
self.textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.textView.textAlignment = NSTextAlignmentRight;
self.textView.textColor = [UIColor lightBlueColor];
self.textView.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:17];
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.textView.keyboardType = UIKeyboardTypeDefault;
[self addSubview:self.textView];
self.textView.delegate = self;
}
- (BOOL)resignFirstResponder {
if (_delegate && [_delegate respondsToSelector:#selector(tableViewCell:didEndEditingWithLongString:)]) {
[_delegate tableViewCell:self didEndEditingWithLongString:self.stringValue];
}
return [super resignFirstResponder];
}
- (void)setKeyboardType:(UIKeyboardType)keyboardType
{
self.textView.keyboardType = keyboardType;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initalizeInputView];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initalizeInputView];
}
return self;
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
[self.textView becomeFirstResponder];
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
[self.textView becomeFirstResponder];
}
}
- (void)setStringValue:(NSString *)value {
self.textView.text = value;
}
- (NSString *)stringValue {
return self.textView.text;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// For keyboard scroll
UITableView *tableView = (UITableView *)self.superview;
AppSetupViewController *parent = (AppSetupViewController *)_delegate;
parent.activeCellIndexPath = [tableView indexPathForCell:self];
}
- (void)textViewDidChange:(UITextView *)textView
{
if (textView.contentSize.height > contentRowHeight) {
contentRowHeight = textView.contentSize.height;
UITableView *tableView = (UITableView *)self.superview;
[tableView beginUpdates];
[tableView endUpdates];
[textView setFrame:CGRectMake(0, 0, 300.0, textView.contentSize.height)];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if (_delegate && [_delegate respondsToSelector:#selector(tableViewCell:didEndEditingWithLongString:)]) {
[_delegate tableViewCell:self didEndEditingWithLongString:self.stringValue];
}
UITableView *tableView = (UITableView *)self.superview;
[tableView deselectRowAtIndexPath:[tableView indexPathForCell:self] animated:YES];
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect editFrame = CGRectInset(self.contentView.frame, 10, 10);
if (self.textLabel.text && [self.textLabel.text length] != 0) {
CGSize textSize = [self.textLabel sizeThatFits:CGSizeZero];
editFrame.origin.x += textSize.width + 10;
editFrame.size.width -= textSize.width + 10;
self.textView.textAlignment = NSTextAlignmentRight;
} else {
self.textView.textAlignment = NSTextAlignmentLeft;
}
self.textView.frame = editFrame;
}
Which is created in cellForRowAtIndexPath like this:
else if ([paramType isEqualToString:#"longString"]) {
MyIdentifier = #"AppActionLongString";
LongStringInputTableViewCell *cell = (LongStringInputTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
cell.textLabel.text = [[[_selectedAction objectForKey:#"parameters"] objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.params = [[_selectedAction objectForKey:#"parameters"] objectAtIndex:indexPath.row];
cell.textView.text = [results objectAtIndex:indexPath.row];
return cell;
}
Simply passing back the height to a variable in my ViewController is no good because like I said, there could be several of these cells within the table.
Thanks
Use this method to dynamically resize your tableviewCell. First store the user input in NSMutable Array and after that reload table. Hope it will help you.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *msg =[self.messages objectAtIndex:indexPath.row];
CGSize textSize = { 120, 10000.0 };
CGSize size = [msg sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:textSize
lineBreakMode:UILineBreakModeWordWrap];
return size.height+20;
}
I needed a dynamic table view cell height based on the amount of text to be displayed in that cell. I solved it in this way:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!isLoading)
{
if ([self.conditionsDataArray count]>0)
{
Conditions *condition =[self.conditionsDataArray objectAtIndex:indexPath.row];
int height;
UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 236, 0)]; //you can set your frame according to your need
textview.text = condition.comment;
textview.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[tableView addSubview:textview];
textview.hidden = YES;
height = textview.contentSize.height;
NSLog(#"TEXT VIEW HEIGHT %f", textview.contentSize.height);
[textview removeFromSuperview];
[textview release];
return height;
}
return 55; //Default height, if data is in loading state
}
Notice that the Text View has been added as Subview and then made hidden, so make sure you add it as SubView otherwise it's height will not be considered.
It would be easy if I could call the cell in question during heightForRowAtIndexPath but that causes an infinite loop and crash as this method is called before any cell's are even created. Any other solutions?
You can. I would guess you're attempting to call cellForRowAtIndexPath, which will cause an infinite loop. But you should rather be dequeuing the cell directly by calling dequeueReusableCellWithIdentifier.
See the table view delegate implementation of TLIndexPathTools. The heightForRowAtIndexPath method looks like this:
(EDIT Initially forgot to include the method prototypeForCellIdentifier that actually dequeues the cell.)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id item = [self.dataModel itemAtIndexPath:indexPath];
NSString *cellId = [self cellIdentifierAtIndexPath:indexPath];
if (cellId) {
UITableViewCell *cell = [self prototypeForCellIdentifier:cellId];
if ([cell conformsToProtocol:#protocol(TLDynamicSizeView)]) {
id<TLDynamicSizeView> v = (id<TLDynamicSizeView>)cell;
id data;
if ([item isKindOfClass:[TLIndexPathItem class]]) {
TLIndexPathItem *i = (TLIndexPathItem *)item;
data = i.data;
} else {
data = item;
}
CGSize computedSize = [v sizeWithData:data];
return computedSize.height;
} else {
return cell.bounds.size.height;
}
}
return 44.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView prototypeForCellIdentifier:(NSString *)cellIdentifier
{
UITableViewCell *cell;
if (cellIdentifier) {
cell = [self.prototypeCells objectForKey:cellIdentifier];
if (!cell) {
if (!self.prototypeCells) {
self.prototypeCells = [[NSMutableDictionary alloc] init];
}
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//TODO this will fail if multiple tables are being used and they have
//overlapping identifiers. The key needs to be unique to the table
[self.prototypeCells setObject:cell forKey:cellIdentifier];
}
}
return cell;
}
This uses a protocol TLDynamicSizeView that any cell can implement to have it's height calculated automatically. Here is a working example project. The cell's implementation of the protocol looks like this:
#implementation DynamicHeightCell
- (void)awakeFromNib
{
[super awakeFromNib];
self.originalSize = self.bounds.size;
self.originalLabelSize = self.label.bounds.size;
}
- (void)configureWithText:(NSString *)text
{
self.label.text = text;
[self.label sizeToFit];
}
#pragma mark - TLDynamicSizeView
- (CGSize)sizeWithData:(id)data
{
[self configureWithText:data];
//the dynamic size is calculated by taking the original size and incrementing
//by the change in the label's size after configuring
CGSize labelSize = self.label.bounds.size;
CGSize size = self.originalSize;
size.width += labelSize.width - self.originalLabelSize.width;
size.height += labelSize.height - self.originalLabelSize.height;
return size;
}
#end
just comment
if (cell == nil)
Hope, this will help you.
I have UITableViewCell subview as UIScrollview and UIscrollview as dynamic uilabels and i need to scroll horizontally with pagination. but i need to scroll synchronously all the table view cell. problem is not able to scroll all the cell togeather.
here is my source code.
Customcell source:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//ScrollView
self.kpiScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 300, 70)];
[self.kpiScrollView setPagingEnabled:YES];
[self.contentView addSubview:self.kpiScrollView];
[self.kpiScrollView release];
NSArray *colors = [NSArray arrayWithObjects:[UIColor grayColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i =0; i<colors.count; i++) {
CGRect frame;
frame.origin.x = self.kpiScrollView.frame.size.width *i;
frame.origin.y = 0;
frame.size = self.kpiScrollView.frame.size;
subView = [[UIView alloc] initWithFrame:frame];
subView.backgroundColor = [colors objectAtIndex:i];
[self.kpiScrollView addSubview:subView];
[subView release];
}
self.kpiScrollView.contentSize =
CGSizeMake(self.kpiScrollView.frame.size.width*colors.count,
self.kpiScrollView.frame.size.height);
}
}
and TableView source:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"CellIdentifier";
PageCell *cell = (PageCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[PageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
}
// cell.pageDelegate = self;
cell.self.kpiScrollView.delegate= self;
cell.tag = indexPath.row+1;
NSLog(#"cell tag:%d", cell.tag);
return cell;
}
UIScrollView delegate methods:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (kpiScrollView == self.kpiTableView) {
return;
}
CGPoint contentOffset = kpiScrollView.contentOffset;
for (PageCell *cell in [self.kpiTableView visibleCells]) {
cell.kpiScrollView.contentOffset = contentOffset;
}
}
Skimming the code, not sure if there are other problems, but the first thing to fix is where you get the content offset....
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// note the change here...
if (sender == self.kpiTableView) return;
// get the content offset from the cell's scrollview that posted this delegate message
CGPoint contentOffset = sender.contentOffset;
for (PageCell *cell in [self.kpiTableView visibleCells]) {
cell.kpiScrollView.contentOffset = contentOffset;
}
}