I have a small issue with the multiple CustomCells by using SegmentedControl. I have imported a framework HMSegmentedControl with 4 segmentedcontrols like (Admin, Engineer, Doctor, Employee). Now the problem is how to use multiple custom cells like (AdminCellList, EngineerCellList, DoctorCellList, EmployeeListCell). When Admin SegmentedControl is Clicked AdminCellList should be loaded and so on. Following is what i have tried. TIA.
MYViewController.h
#interface MYViewController : UIViewController
{
NSUInteger currentScreen;
}
MYViewController.m
self.AdminView.hidden = NO;
self.EngineerView.hidden = YES;
self.DoctorView.hidden = YES;
self.EmployeeView.hidden = YES;
currentScreen=0;
[self.scrollView addSubview:self.AdminView];
self.edgesForExtendedLayout = UIRectEdgeNone;
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:#[#"Admin", #"Engineer", #"Doctor", #"Employee"]];
segmentedControl.frame = CGRectMake(0, 0, viewWidth, 45);
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
segmentedControl.backgroundColor = [UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
segmentedControl.selectionIndicatorColor = [UIColor blackColor];
segmentedControl.selectionIndicatorHeight = 2.0f;
segmentedControl.verticalDividerEnabled = YES;
segmentedControl.verticalDividerColor = [UIColor blackColor];
segmentedControl.verticalDividerWidth = 1.0f;
segmentedControl.borderColor = [UIColor blackColor];
[segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
return attString;
}];
[segmentedControl addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
}
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment
{
if(segment.selectedSegmentIndex == 0) {
currentScreen=0;
//Loading Service
}
else if(segment.selectedSegmentIndex == 1) {
currentScreen=1;
//Loading Service
}
else if(segment.selectedSegmentIndex == 2) {
currentScreen=2;
//Loading Service
}
else if(segment.selectedSegmentIndex == 3) {
currentScreen=3;
//Loading Service
}
}
- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
NSLog(#"Selected index %ld", (long)segmentedControl.selectedSegmentIndex);
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
AdminCellList *cell = (AdminCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AdminCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AdminCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
Everything looks good, just need some additions:
1.Reload table each time user changes segment.
2.Put check inside cellForRowAtIndexPath according to currentScreen and load corresponding cell with custom class.
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment {
if(segment.selectedSegmentIndex == 0) {
currentScreen=0;
}
else if(segment.selectedSegmentIndex == 1) {
currentScreen=1;
}
....... more code
[myTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
if (currentScreen == 0) {
AdminCellList *cell = (AdminCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AdminCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AdminCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
}
}
} else if (currentScreen == 1) {
//Same code for cell as AdminCellList
EngineerCellList *cell = (EngineerCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"EngineerCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (EngineerCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
}
}
} else if (currentScreen == 2) {
//Similar custom cell code here as above
} else {
//Similar custom cell code here as above
}
}
Related
This question already has answers here:
Reducing the space between sections of the UITableView
(11 answers)
Closed 5 years ago.
I am designing a uitableview with multiple sections and row in that section i have expand and collapse functionality for that.Now when the row is expanded I have give space between row and section like below image.
here is my code for expand and collapsing the the section.and for section i use header view.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return menuarray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return menuarray.count;
if ([arrSelectedSectionIndex containsObject:[NSNumber numberWithInteger:section]])
{
NSArray *arr=[menuarray[section] valueForKey:#"menu"];
return arr.count;
}else{
return 0;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MenudetailsTblCell";
menudetailscell = ( MenudetailsTblCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// [self.tblview registerNib:[UINib nibWithNibName:#"MenudetailsTblCell" bundle:nil]
//forCellReuseIdentifier:#"MenudetailsTblCell"];
if (menudetailscell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenudetailsTblCell" owner:self options:nil];
menudetailscell = [nib objectAtIndex:0];
}
if (menudetailscell == nil)
{
menudetailscell = [[MenudetailsTblCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
menudetailscell.contentView.layer.cornerRadius = 5;
menudetailscell.contentView.layer.masksToBounds = YES;
NSArray *arr=[menuarray[indexPath.section] valueForKey:#"menu"];
menudetailscell.typelbl.text=[[arr objectAtIndex:indexPath.row]valueForKey:#"name"];
menudetailscell.describtionlbl.text=[[arr objectAtIndex:indexPath.row]valueForKey:#"description"];
NSString *cost=[NSString stringWithFormat:#"%#%#",[[arr objectAtIndex:indexPath.row]valueForKey:#"price"],#".00"];
NSString *imgid=[NSString stringWithFormat:#"%#",[[arr objectAtIndex:indexPath.row]valueForKey:#"idmenus"]];
NSString *tax=[NSString stringWithFormat:#"%#",[[arr objectAtIndex:indexPath.row]valueForKey:#"tax"]];
if ([tax isEqualToString:#"tax"]) {
menudetailscell.taxlbl.text=#"(*Inclusive all taxes)";
}else{
menudetailscell.taxlbl.text=#"(*Exclusive all taxes)";
}
menudetailscell.ratelbl.text=[NSString stringWithFormat:#"%# %#",[[arr objectAtIndex:indexPath.row]valueForKey:#"country"],cost];
NSString *typeimgUrl1 = [NSString stringWithFormat:#"%s/presignin/menu/showMedia?idMenu=%#",urlPath,imgid];
NSURL *imageURL=[NSURL URLWithString:typeimgUrl1];
menudetailscell.imgview.imageURL=imageURL;
menudetailscell.selectionStyle = UITableViewCellSelectionStyleNone;
return menudetailscell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 70.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *CellIdentifier = #"Menusectioncell";
menusectioncell = ( Menusectioncell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (menusectioncell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Menusectioncell" owner:self options:nil];
menusectioncell = [nib objectAtIndex:0];
}
if (menusectioncell == nil)
{
menusectioncell = [[Menusectioncell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([arrSelectedSectionIndex containsObject:[NSNumber numberWithInteger:section]])
{
menusectioncell.Sectiomtitlebtn.selected = YES;
menusectioncell.dotlbl.hidden=NO;
}
else{
menusectioncell.dotlbl.hidden=YES;
}
menusectioncell.Sectiomtitlebtn.tag=section;
menusectioncell.dotlbl.layer.cornerRadius = menusectioncell.dotlbl.frame.size.height/2;
menusectioncell.dotlbl.layer.masksToBounds = YES;
[menusectioncell.Sectiomtitlebtn addTarget:self action:#selector(btnTapShowHideSection:) forControlEvents:UIControlEventTouchUpInside];
NSString *btntitle=[menuarray[section] valueForKey:#"catagory"];
itemarr=[menuarray[section] valueForKey:#"menu"];
NSString *items=[itemarr[0] valueForKey:#"name"];
menusectioncell.sectiontitleilb.text=btntitle;
menusectioncell.itemslbl.text=[NSString stringWithFormat:#"%#.,etc...",items];
menusectioncell.selectionStyle = UITableViewCellSelectionStyleNone;
return menusectioncell.contentView;
}
-(IBAction)btnTapShowHideSection:(UIButton*)sender
{
if (!sender.selected)
{
if (!isMultipleExpansionAllowed) {
[arrSelectedSectionIndex replaceObjectAtIndex:0 withObject:[NSNumber numberWithInteger:sender.tag]];
}else {
[arrSelectedSectionIndex addObject:[NSNumber numberWithInteger:sender.tag]];
}
menusectioncell.dotlbl.hidden=YES;
sender.selected = YES;
}
else{
sender.selected = NO;
menusectioncell.dotlbl.hidden=NO;
if ([arrSelectedSectionIndex containsObject:[NSNumber numberWithInteger:sender.tag]])
{
[arrSelectedSectionIndex removeObject:[NSNumber numberWithInteger:sender.tag]];
}
}
if (!isMultipleExpansionAllowed) {
[_tblview reloadData];
}else {
[_tblview reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
I just wanted to give space between maincourse and down details for headerview and rows I am using 2 different cell. Please help me out to solve this.
then just return more height in heightForHeaderInSection. For example 80 instead of 70. that's it!
And you could manage height of menusectioncell.contentView also according to header height!
I've been trying to add a new row dynamically in a UITableView when the user taps on the row just above. There seem to be 2-3 other posts around this but am somehow not able to connect / leverage the same.
Following is my code. Could someone give me an idea please where I might be going wrong?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
NSInteger rowsRequired = 0;
if (section == 0) {
rowsRequired = 1;
}
else {
rowsRequired = [[self contents] count] + 1;
}
return rowsRequired;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
CellType1 *cell1 = nil;
CellType2 *cell2 = nil;
if (indexPath.section == 0) {
cell1 = [tableView dequeueReusableCellWithIdentifier:#“CellType1”];
if (cell1 == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#“CellType1” owner:self options:nil];
cell1 = [nib objectAtIndex:0];
}
return cell1;
}
else {
cell2 = [tableView dequeueReusableCellWithIdentifier:#“CellType2”];
if (cell2 == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#“CellType2” owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
return cell2;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
indexPath = newPath;
[[self contents] insertNewRow:#“My New Row”];
[[self tableView] insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I think you have to insert the new value in the array after the current index path value
[contents insertObject:myObject atIndex:10];
I'm setting the placeholders for a cell inside the if(cell == nil) condition.
Still, when I scroll my table, The placeholder values get interchanged. Please help on this.
Here is the code from CellForRowAtIndexpath: Please let me know where I'm mistaking..
static NSString *cellIdentifierCell = #"MTRegisterTableViewCell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:cellIdentifierCell] owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if(indexPath.row==6)
{
if (cell.txtRegister.text.length == 0) {
if (cell.txtRegister.placeholder.length == 0) {
cell.txtRegister.placeholder=[self.profileTitlesArray
objectAtIndex:indexPath.row-1];
}
}
NSLog(#"Placeholder :%# && iINDEXPATH.ROW
:%ld",cell.txtRegister.placeholder,(long)indexPath.row);
}
else
{
if (cell.txtRegister.text.length == 0) {
if (cell.txtRegister.placeholder.length == 0) {
cell.txtRegister.placeholder=[self.profileTitlesArray
objectAtIndex:indexPath.row];
}
}
}
}
if(indexPath.row==5)
{
static NSString *cellIdentifierCell = #"MTRegisterDropdownCell";
MTRegisterDropdownCell *cellDrop = [tableView
dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cellDrop == nil)
{
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:cellIdentifierCell] owner:self options:nil];
cellDrop = (MTRegisterDropdownCell *)[nib objectAtIndex:0];
[cellDrop configureCell:nil];
}
self.countryCell=cellDrop;
cellDrop.clipsToBounds=NO;
self.tblDropdown.clipsToBounds=NO;
[cellDrop.btnCountry addTarget:self action:#selector(btnCountryClicked:)
forControlEvents:UIControlEventTouchUpInside];
[cellDrop.btnState addTarget:self action:#selector(ButtonStateClicked:)
forControlEvents:UIControlEventTouchUpInside];
if(self.strSelectedCountry)
{
[cellDrop.txtCountry setText:self.strSelectedCountry];
}
else{
if ([self.countriesArray containsObject:kDefaultCountry]) {
[cellDrop.txtCountry setText:kDefaultCountry];
NSInteger index = [self.countriesArray
indexOfObject:kDefaultCountry];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index
inSection:0];
self.tblDropdown.tag=TABLECOUNTRYTAG;
[self tableView:self.tblDropdown didSelectRowAtIndexPath:indexPath];
}
}
if(self.strSelectedState)
{
[cellDrop.txtState setText:self.strSelectedState];
}
return cellDrop;
}
else
{
if (cell.txtRegister.text.length == 0) {
NSLog(#"Placeholder :%#",cell.txtRegister.placeholder);
if ([cell.txtRegister.placeholder isEqualToString:#"City"]) {
if (self.strSelectedCity) {
cell.txtRegister.text = self.strSelectedCity;
}
else
cell.txtRegister.text=[self.profileTitlesArray
objectAtIndex:indexPath.row];
NSLog(#"profileTitlesArray :%#",self.profileTitlesArray);
NSLog(#"indexpath.Row :%ld && cell TEXT :%#",
(long)indexPath.row,cell.txtRegister.text);
BaseButton *aBtn = [[BaseButton alloc]
initWithFrame:CGRectMake(cell.txtRegister.frame.origin.x-50,
cell.txtRegister.frame.origin.y-10, cell.txtRegister.frame.size.width+50,
cell.txtRegister.frame.size.height+50)];
[aBtn setBackgroundColor:[UIColor clearColor]];
[cell.txtRegister addSubview:aBtn];
[aBtn addTarget:self action:#selector(btnCitiesClicked:)
forControlEvents:UIControlEventTouchUpInside];
self.cityCelll = cell;
cell.clipsToBounds = NO;
}
}
else{
if ([cell.txtRegister.placeholder isEqualToString:#"City"]) {
if (self.strSelectedCity) {
cell.txtRegister.text = self.strSelectedCity;
}
}
}
}
[cell configureCell:registerModel];
return cell;
}
Did not read you whole code, but it sounds like you'll need to reset the placeholders before a cell gets reused, they are not necessarly recreated on scroll, that is the point of a table view after all.
See -[UITableViewCell prepareForReuse]. It is called upon dequeue / reuse an existing table cell, so technically when you call -[UITableView dequeueReusableCellWithIdentifier:] (You'll need to subclass UITableViewCell, it organizes your code further anyway).
I finally solved it by using following line:
NSString *cellIdentifierCell = [NSString
stringWithFormat:#"MTRegisterTableViewCell %ld",(long)indexPath.row];
cell = (MTRegisterTableViewCell *)[tableView
dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:#"MTRegisterTableViewCell"] owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
Could any one help me? Am working on expanding cell for the past one week Finally i can able to add sub menu in it. I designed two custom cells and using plist i added menu and sub menu to that. It is working well i added menu and sub menu. Now my problem is i want to add image and button to row 1,2,4,6 only using indexpath.row i assigned but this code assigning image and button to all rows But i only want to add to row 1,2,4,6 only ho i can do this pls some one help me???
interface MyHomeView ()
{
NSMutableArray *LeftPaneList;
NSArray *datalist;
}
#property (assign)BOOL isOpen;
#property (nonatomic,retain)NSIndexPath *selectIndex;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"LeftPaneMenuList" ofType:#"plist"];
LeftPaneList = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.isOpen=NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [LeftPaneList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.isOpen) {
if (self.selectIndex.section == section) {
return [[[LeftPaneList objectAtIndex:section] objectForKey:#"SubMenu"] count]+1;;
}
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"MyHomeViewCell2";
MyHomeViewCell2 *cell = (MyHomeViewCell2*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[LeftPaneList objectAtIndex:self.selectIndex.section] objectForKey:#"SubMenu"];
cell.name.text = [list objectAtIndex:indexPath.row-1];
return cell;
}
else
{
static NSString *CellIdentifier = #"MyHomeViewCell";
MyHomeViewCell *cell = (MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
cell.tag=indexPath.row;
NSString *name = [[LeftPaneList objectAtIndex:indexPath.section] objectForKey:#"MenuName"];
cell.MyHomeMenuLabel.text = name;
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert
{
self.isOpen = firstDoInsert;
[self.MyHomeTableView beginUpdates];
int section = self.selectIndex.section;
int contentCount = [[[LeftPaneList objectAtIndex:section] objectForKey:#"SubMenu"] count];
NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
for (NSUInteger i = 1; i < contentCount + 1; i++) {
NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section];
[rowToInsert addObject:indexPathToInsert];
}
if (firstDoInsert)
{ [self.MyHomeTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[self.MyHomeTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
[self.MyHomeTableView endUpdates];
if (nextDoInsert) {
self.isOpen = YES;
self.selectIndex = [self.MyHomeTableView indexPathForSelectedRow];
[self didSelectCellRowFirstDo:YES nextDo:NO];
}
if (self.isOpen) [self.MyHomeTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 52;
}
This is the original o/p!
But i want o/p like this
some one help me??
you need to specify IndexPath.section First then you can check with IndexPath.row like Bellow Example:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if(indexPath.section==0)
{
if(indexPath.row==0)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==4)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==6)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
}
}
Th easy way to do this, is to set up 2 different dynamic prototype cells in the storyboard, each with its own identifier. In cellForRowAtIndexPath: dequeue the correct type of cell based on the indexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.row = 1 || indexPath.row = 2 || indexPath.row = 4 || indexPath.row = 6){
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}else{
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
As you are using dequeueReusableCellWithIdentifier: method, it will just give cell at index path 2, 4, 6 etc to some other cell in tableView. Instead use array to store your cell and then retrieve it from array. And then you can use indexpath.row
How wrong could it be the code bellow:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
NSArray *array = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id currentObject in array) {
if ([currentObject isKindOfClass:[self class]]) {
self = [currentObject retain];
}
}
return self;
}
I'm trying to bring up a custom UITableViewCell from nib file, the problem is that Leaks Instruments says there's a leak inside:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CPVideoGenTVCell *cell = (CPVideoGenTVCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CPVideoGenTVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.delegate = self;
cell.titleLbl.textColor = [UIColor colorWithRed:0.992 green:0.933 blue:0.0 alpha:1.0];
}
CPTempVideo *aVideo = [self.videos objectAtIndex:indexPath.row];
cell.seasonLbl.text = [NSString stringWithFormat:#"Season %#", [self romanNumberFromArabic:aVideo.season]];
cell.titleLbl.text = [NSString stringWithFormat:#"%2i - \"%#\"",aVideo.idVideo, aVideo.chapterName];
cell.managedImg.url = aVideo.thumbnail;
[[CPDataManager sharedDataManager] manageImage:cell.managedImg];
return cell;
}
think the problem is with retain, but don't understand why are you doing this crazy stuff with initWithStyle..
if ([currentObject isKindOfClass:[self class]]) {
self = currentObject;
}
There's an alternative I've found:
+ (id)cellWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
NSArray *array = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id currentObject in array) {
if ([currentObject isKindOfClass:[self class]]) {
return currentObject;
}
}
return nil;
}
just return an autoreleased object and take care of that inside cellForRowAtIndexPath:
CPTempHeadCell *headCell = (CPTempHeadCell *)[tableView dequeueReusableCellWithIdentifier:HeadCellIdentifier];
if (headCell == nil) {
headCell = [CPVideoGenTVCell cellWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];