UILabel showing wrong - ios

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.

Related

The cell of tavbview quickly slides into a blank

A tabview pull up, load the new data, and then quickly pull up, the above cell disappears and becomes blank,I guess because the refresh time, the data is not with the new
This is the main code:
#interface DYBOrdersViewController ()<UITableViewDelegate,UITableViewDataSource>
#property(nonatomic,strong)NSMutableArray * currentArr;
#property(nonatomic,strong)NSArray * dataArr;
#property(nonatomic,strong)UITableView * mainTab;
#property(nonatomic,strong)UIRefreshControl * refreshC;
#property(nonatomic,assign)BOOL isPull;
#property(nonatomic,assign)CGFloat cellheight;
#end
static NSString * orderID = #"orderID";
#implementation DYBOrdersViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray * mutarr0 = [NSMutableArray arrayWithObjects:#"全部",#"全部",#"全部",#"完成",#"全部", nil];
NSMutableArray * mutarr1 = [NSMutableArray arrayWithObjects:#"待取件",#"待取件",#"待取件",#"待取件", nil];
NSMutableArray * mutarr2 = [NSMutableArray arrayWithObjects:#"未完成",#"未完成",#"未完成", nil];
NSMutableArray * mutarr3 = [NSMutableArray arrayWithObjects:#"已完成",#"已完成", nil];
NSMutableArray * mutarr4 = [NSMutableArray arrayWithObjects:#"已取消", nil];
self.dataArr = #[mutarr0,mutarr1,mutarr2,mutarr3,mutarr4];
self.currentArr = self.dataArr[0];
[self setUI];
self.automaticallyAdjustsScrollViewInsets = NO;
}
// 设置界面
-(void)setUI{
UIRefreshControl * refreshC = [[UIRefreshControl alloc]initWithFrame:self.navBar.bounds];
self.refreshC = refreshC;
[refreshC beginRefreshing];
[refreshC addTarget:self action:#selector(loadData) forControlEvents:UIControlEventValueChanged];
NSArray * segArr = #[#"全部",#"待取件",#"待收货",#"待评价",#"已取消"];
UISegmentedControl * segC = [[UISegmentedControl alloc]initWithItems:segArr];
segC.frame = CGRectMake(0,64, self.view.frame.size.width,40);
segC.tintColor = [UIColor sun_colorWithHex:0x0082f5];
//实现添加的事件
[segC addTarget:self action:#selector(change:) forControlEvents:UIControlEventValueChanged];
// 创建刷新界面
UITableView * tabV = [[UITableView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(segC.frame), [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height - CGRectGetMaxY(segC.frame)-self.tabBarController.tabBar.frame.size.height) style:UITableViewStylePlain];
tabV.separatorStyle = UITableViewCellSeparatorStyleNone;
tabV.showsVerticalScrollIndicator = NO;
tabV.delegate = self;
tabV.dataSource = self;
self.mainTab = tabV;
[tabV registerClass:[DYBOrdersViewCell class] forCellReuseIdentifier:orderID];
[tabV addSubview:self.refreshC];
[self.view addSubview:tabV];
[self.view addSubview:segC];
}
// Refresh data method
-(void)loadData{
[self.refreshC endRefreshing];
[self.currentArr addObject:#"新数据"];
self.isPull = NO;
[self.mainTab reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
//点击不同分段就会有不同的事件进行相应
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(#"%lu",(unsigned long)self.currentArr.count);
return self.currentArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DYBOrdersViewCell * cell = [tableView dequeueReusableCellWithIdentifier:orderID forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// cell.textLabel.text = self.currentArr[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DYBOrderdetailController * VC = [[DYBOrderdetailController alloc]init];
VC.title = #"订单详情";
VC.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:VC animated:YES];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
// 将要显示的最后一组
NSInteger section = indexPath.section;
// 所有cell数
NSInteger count = [tableView numberOfRowsInSection:section];
//将要显示的row数量
NSInteger row = indexPath.row ;
if (section <0 || row <0) {
return;
}
// 如果是最后一行最后一个cell 数据刷新
if (row == count -1 && !self.isPull) {
NSLog(#"要刷新了");
self.isPull = YES;
[self loadData];
}
}

UICollectionView Horizontal Scrollling Delay loading cells

Trying to add a horizontal Scrolling UICollectionView of times. The problem is after a bunch of cells, it's like the next ones are loading late. I have to frequently scroll all the way over the screen until the next group of cell will load. In the screen shot, the times 2:30PM & 3:00PM should already be visible.
View Controller
GHTMeetingTimesCollectionViewFlowLayout *timesFlowLayout = [[GHTMeetingTimesCollectionViewFlowLayout alloc] init];
timesFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
timesFlowLayout.itemSize = CGSizeMake(kIconCellItemWidth, kIconCollectionHeight);
self.timesSelectCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:timesFlowLayout];
self.timesSelectCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
self.timesSelectCollectionView.allowsMultipleSelection = YES;
self.timesSelectCollectionView.showsHorizontalScrollIndicator = NO;
self.timesSelectCollectionView.backgroundColor = [UIColor clearColor];
self.timesSelectCollectionView.delegate = self;
self.timesSelectCollectionView.dataSource = self;
[self.contentView addSubview:self.timesSelectCollectionView];
[self.timesSelectCollectionView registerClass:[GHTMeetingTimeCollectionViewCell class] forCellWithReuseIdentifier:kMeetingTimeCellIdentifier];
GHTMeetingTimesCollectionViewFlowLayout
#implementation GHTMeetingTimesCollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* arr = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* atts in arr) {
if (nil == atts.representedElementKind) {
NSIndexPath* attsPath = atts.indexPath;
atts.frame = [self layoutAttributesForItemAtIndexPath:attsPath].frame;
}
}
return arr;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes* atts = [super layoutAttributesForItemAtIndexPath:indexPath];
CGRect adjustedFrame = atts.frame;
adjustedFrame.origin.x = indexPath.row*kIconCellItemWidth;
adjustedFrame.size.width = kIconCellItemWidth;
adjustedFrame.size.height = kIconCollectionHeight;
atts.frame = adjustedFrame;
return atts;
}
CollectionView Cell Class
#implementation GHTMeetingTimeCollectionViewCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.highlightColor = [UIColor lightBlue];
self.timeLabel = [[UILabel alloc] init];
self.timeLabel.layer.cornerRadius = 3;
self.timeLabel.clipsToBounds = YES;
self.timeLabel.font = [UIFont fontWithName:GFFontStandard size:14];
self.timeLabel.textColor = [UIColor lightBlue];
self.timeLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.timeLabel.textAlignment = NSTextAlignmentCenter;
self.timeLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.timeLabel];
[NSLayoutConstraint sidesOfChild:self.timeLabel toSidesOfParent:self.contentView margin:8];
[NSLayoutConstraint centerYOfChild:self.timeLabel toCenterYOfParent:self.contentView];
}
return self;
}
-(void)setTimeDisplay:(NSString *)time {
self.timeLabel.text = time;
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
self.timeLabel.backgroundColor = selected ? self.highlightColor : [UIColor clearColor];
self.timeLabel.textColor = selected ? [UIColor whiteColor] : self.highlightColor;
}
- (void)setHighlightColor:(UIColor *)highlightColor {
_highlightColor = highlightColor;
self.timeLabel.backgroundColor = self.isSelected ? _highlightColor : [UIColor clearColor];
self.timeLabel.textColor = self.isSelected ? [UIColor whiteColor] : _highlightColor;
}
Delegate method
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
GHTMeetingTimeCollectionViewCell *cell = [self.timesSelectCollectionView dequeueReusableCellWithReuseIdentifier:kMeetingTimeCellIdentifier forIndexPath:indexPath];
NSDate *date = [self.times objectAtIndex:indexPath.row];
[cell setTimeDisplay:[self.timeOnlyFormatter stringFromDate:date]];
if (self.existing) {
[cell setHighlightColor:[UIColor lightGrayColor]];
}
return cell;
}
Try it that way :
#interface GHTMeetingTimesCollectionViewFlowLayout ()
#property (nonatomic, strong) NSMutableDictionary *layoutInfo;
#end
#implementation GHTMeetingTimesCollectionViewFlowLayout
- (NSDictionary *)layoutInfo
{
if (!_layoutInfo)
{
_layoutInfo = [NSMutableDictionary dictionary];
}
return _layoutInfo;
}
- (void)prepareLayout
{
NSInteger sectionCount = [self.collectionView numberOfSections];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
for (NSInteger section = 0; section < sectionCount; section++) {
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
for (NSInteger item = 0; item < itemCount; item++) {
indexPath = [NSIndexPath indexPathForItem:item inSection:section];
UICollectionViewLayoutAttributes *atts = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGRect adjustedFrame = atts.frame;
adjustedFrame.origin.x = indexPath.row*kIconCellItemWidth;
adjustedFrame.size.width = kIconCellItemWidth;
adjustedFrame.size.height = kIconCollectionHeight;
atts.frame = adjustedFrame;
self.layoutInfo[indexPath] = atts;
}
}
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.layoutInfo.count];
[self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *innerStop)
{
if (CGRectIntersectsRect(rect, attributes.frame)) {
[allAttributes addObject:attributes];
}
}];
return allAttributes;
}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.layoutInfo[indexPath];
}
#end
PS : I just wrote that, didn't test it. I hope it doesn't contain any error. But it any case, it's the way you should handle custom flow layouts.

tableview doesn't see the title for header in section method

I was trying to create a custom viewForHeaderInSection for my tableview.Tableview itself
works fine, but for some reason, compiler skips,
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{}
Delegate method, and I tried what is written on the reference , and this .But for some reason it just doesn't read these methods. Obviously, I am doing something wrong. If someone can point my mistake out, I would appreciate. Thank you in advance.
#import "TableViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
#synthesize settingTableView,audioPlayer,labelSwitch,descriptiveSwitch,autoPlaySwitch;;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.settingTableView.tableFooterView = [[UIView alloc]init];
self.settingTableView.scrollEnabled = NO;
settings = #{#"Animation Sets" : #[#"Set1",#"Set2",#"Set3"],#"Settings" : #[#"Label",#"Sound",#"Autoplay"]};
values = [[settings allKeys]sortedArrayUsingSelector:#selector(localizedStandardCompare:)];
[settingTableView setRowHeight:44];
[settingTableView reloadData];
}
-(void)close:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
[self.delegate closeView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [values count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [values objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = [values objectAtIndex:section];
NSArray *sectionValue = [settings objectForKey:sectionTitle];
return [sectionValue count];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGRect frame = tableView.frame;
NSString *value = [values objectAtIndex:section];
UILabel *title = [[UILabel alloc]init];
UIView *headerView = [[UIView alloc]init];
//if (section == 0 && section == 1) {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
title.frame = CGRectMake(10,10,150,30);
}
else
{
title.frame = CGRectMake(10,5,100,30);
}
title.backgroundColor = [UIColor clearColor];
title.text = value;
headerView.frame = CGRectMake(0,0,frame.size.width,frame.size.height);
headerView.backgroundColor = [UIColor clearColor];
[headerView addSubview:title];
[headerView setBackgroundColor:[UIColor blueColor]];
//}
return headerView;
[settingTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = nil;
static NSString *MyCellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyCellIdentifier];
cell.textLabel.numberOfLines = 2;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
NSString *sectionTitle = [values objectAtIndex:indexPath.section];
NSArray *sectionValue= [settings objectForKey:sectionTitle];
NSString *settingValues = [sectionValue objectAtIndex:indexPath.row];
cell.textLabel.text = settingValues;
return cell;
}
}
You need to make sure that the class that implements that delegate method is the delegate and dataSource of your table view.

CollectionView memory VM:CoreAnimation memory allocated and abandoned

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

Segmented Control change tableView datasource

UISegmentedControl changes tableView datasource.
After I described [myView addSubview:_tableView];
instead of [self.view addSubview:_tableView];,
daySegmentedControl process stopped working.
I have this code.
ViewController.m
#implementation ViewController
{
int _tableType;
NSArray *_data1;
NSArray *_data2;
}
#synthesize segment;
#synthesize daySegment;
#synthesize myView;
- (void)viewDidLoad
{
[super viewDidLoad];
_tableType = 1;
_data1 = #[#[#"A",#"B",#"C"]];
_data2 = #[#[#"D",#"E",#"F"]];
myView = [[UIView alloc]initWithFrame:CGRectMake(-1, 44, 340, 480)];
[self.view addSubview:myView];
myView.opaque = NO;
myView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f];
[self.view bringSubviewToFront:myView];
[self segmentView];
[self daySegmentView];
[self dayTableView];
}
- (void)segmentView
{
NSArray *SegmentContent = [NSArray arrayWithObjects:#"View1",#"View2",nil];
segment = [[UISegmentedControl alloc] initWithItems:WDSegmentContent];
segment.frame = CGRectMake(-2, 20, 326, 25);
segment.selectedSegmentIndex = 0;
[segment addTarget:self action:#selector(WDSegmentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];
}
- (void)WDSegmentAction:(id)sender
{
switch (segment.selectedSegmentIndex){
case 0:
[self dayTableView];
break;
case 1:
[self dayTableView];
break;
default:
break;
}
}
- (void)daySegmentView
{
NSArray *daySegmentContent = [NSArray arrayWithObjects:#"A",#"D",nil];
daySegment = [[UISegmentedControl alloc] initWithItems:daySegmentContent];
daySegment.frame = CGRectMake(0, 0, 326, 25);
daySegment.selectedSegmentIndex = 0;
[daySegment addTarget:self action:#selector(daySegmentAction:) forControlEvents:UIControlEventValueChanged];
[myView addSubview:daySegment];
}
- (void)daySegmentAction:(id)sender
{
switch (segment.selectedSegmentIndex){
case 0:
_tableType = 1;
[self.tableView reloadData];
break;
case 1:
_tableType = 2;
[self.tableView reloadData];
break;
default:
break;
}
}
- (void)dayTableView
{
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, 320, 480)];
_tableView.dataSource = self;
_tableView.delegate = self;
[myView addSubview:_tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_data1[section]count];
}
- (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];
}
NSString *data;
if (_tableType == 1) {
data = _data1[indexPath.section][indexPath.row];
} else if (_tableType == 2){
data = _data2[indexPath.section][indexPath.row];
}
cell.textLabel.text = data;
return cell;
}
#end
Any idea on how I could fix it?
I used different approach for changing segments. Just for illustration:
in segment changed method:
[_tableView reloadData];
In your numberOfRowsInSection:
if(_segControl.selectedSegmentIndex == 0)
{
return [dataSourceOne count];
}else
{
return [dataSourceTwo count];
}
}
In your heightForRowAtIndexPath
if(_segControl.selectedSegmentIndex == 0) { //one
return 60;
} else { //two
return 70;
}
In your cellForRowAtIndexPath
if(_segControl.selectedSegmentIndex == 0) {
//generate and populate cell for type one
}else
{
//generate and populate cell for type two
}
Use daySegment.selectedSegmentIndex instead of segment.selectedSegmentIndex in
- (void)daySegmentAction:(id)sender

Resources