populate uitableview as subview of uicollectionview - objective-c

I have a problem in populating a uitableview created programmatically as a subview of uicollectionview.
here's my code:
.h file
#import <UIKit/UIKit.h>
#interface TheViewController : UICollectionViewController<UITableViewDataSource>
#property (atomic,strong) IBOutlet UIBarButtonItem *plus;
-(IBAction) addTeamPressed:(id)sender;
#end
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *jsonParsingError = nil;
teamsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.window.bounds.size.width, self.view.window.bounds.size.height)];
NSData *t = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."] options:NSDataReadingMappedIfSafe error:&jsonParsingError];
NSArray *tmp = [NSJSONSerialization JSONObjectWithData:t options:0 error:&jsonParsingError];
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
teams = [[NSMutableArray alloc]init];
[teamsView addSubview:teamCell];
teamsView.delegate = self;
teamsView.dataSource = self;
for(int i =0;i<tmp.count;i++){
[teams addObject:[tmp objectAtIndex:i]];
}
plus.title = #"Done";
[self.view addSubview:teamsView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
// If You have only one(1) section, return 1, otherwise you must handle sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [teams count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.name.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;
}
my tableview is displaying correctly, of course thanks to other functions not mentioned here, but no rows in the subview.
the compiler prints out the number of teams, so it calls the delegate methods, but it doesn't populate the subview...
what 's wrong with this?
thanks in advance
Dario

First you should remove this from your viewDidLoad:
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
[teamsView addSubview:teamCell];
Then make sure that your teams array is retained, just put this line to the header file:
#property (nonatomic, strong) NSMutableArray* teams;
And then use self.teams instead of just teams
At the end of viewDidLoad method you should call [teamsView reloadData];
Also it would be good to change your table data source method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// please make sure that you allocate required object here
cell = [[TeamsNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// please make sure that you do have `name` property in your `TeamsNameCell` class
cell.name.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;
}
Please make sure that your TeamsNameCell class has name property in the header:
#property(nonatomic, strong) UILabel* name;
And also do not forget to allocate this label in the constructor of TeamsNameCell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.name = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,50)];
[self addSubview:self.name];
}
return self;
}
Or Maybe it is better to use default cell's label:
instead of cell.name.text = #"text"; use cell.textLabel.text = #"text";

solution is,
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.labelText.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;

Related

Cell text not turning up-UITableView

I have a UITableView within a View Controller. Very strange because the header is showing up and the header text but not the cell text.
- (void)viewDidLoad
{
table.delegate = self;
selection= [[NSMutableArray alloc] init];
[selection addObject:#"text"];
[selection addObject:#"text"];
[selection addObject:#"text"];
...}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// configure the cell in each row
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSString *cellValue = [selection objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
// cell.textColor=[UIColor blackColor];
return cell;
}
Try adding this line
table.dataSource = self;
This way, all the table view data source methods (including cellForRowAtIndexPath:) are called, so long as your class conforms to the UITableViewDataSource protocol.

Displaying data retrieved from Parse in UITableView

After all progress i made with your answers, my issue changed. So i am changing my question with clearer way. I have an UITableView which is showing my retrieved data from Parse.com. So i made a NSMutableArray for adding objects to that array when they are retrieved. But my problem is even i add objects to NSMutableArray, my table does not show anything but default screen of UITableView. I thing the issue is UITableView is formed before my NSMutableArray got its objects. Here is my code:
Note: The PropertyClass is the class which has the properties of my objects.
At MyTableViewController.h
#interface MyTableViewController : UITableViewController <CLLocationManagerDelegate> {
PFObject *object;
}
#property (strong, nonatomic) IBOutlet UITableView *MyTableView;
#end
At UITableViewController.m
#interface MyTableViewController ()
#property(strong)NSMutableArray *myNSMutableArray;
#end
#implementation MyTableViewController
#synthesize myNSMutableArray,MyTableView;
-(void) retrievingDataFromParse{
PFQuery *query = [PFQuery queryWithClassName:#"MyObjectsClass"];
[query whereKey:#"ObjectsNumber" lessThanOrEqualTo:10];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d scores.", objects.count);
if (objects.count==0) {
NSString *objectError = #"There no object retrieved from Parse";
PropertiesClass *PC = [[PropertiesClass alloc]initWithPropert1:objectError Propert2:nil Propert3:nil Propert4:nil];
[myNSMutableArray addObject:PC];
}
for (int i = 0; i < objects.count; i++) {
object = [objects objectAtIndex:i];
NSString *Propert1 = [object objectForKey:#"Propert1"];
NSNumber *Propert2 = [object objectForKey:#"Propert2"];
NSNumber *Propert3 = [object objectForKey:#"Propert3"];
NSString *Propert4 = [object objectForKey:#"Propert4"];
PropertiesClass *PC = [[PropertiesClass alloc]initWithPropert1:Propert1 Propert2:Propert2 Propert3:Propert3 Propert4:Propert4];
[myNSMutableArray addObject:PC];
};
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.myNSMutableArray = [NSMutableArray array];
[self retrievingDataFromParse];
[MyTableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [myNSMutableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PropertiesClass *PC= [myNSMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text=PC.Propert1;
return cell;
}
Looking at your code i see that you never create a UITableViewCell, you should change this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PropertyClass *PC = [myMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = PC.x;
return cell;
}
with this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (nil == cell){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
PropertyClass *PC = [myMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = PC.x;
return cell;
}
the method dequeueReusableCellWithIdentifier:forIndexPath: return a UITableViewCell only if there are unused, but already allocated, cells in your table view. otherwise it returns nil.
Also when you update the mutable array containing all your data you should call [yourTableView reloadData] to force the table view to reload its content.
Your code is quite cryptic. Few suggestions here.
First, rename variables and methods with camelCaseNotation (camel case notation). For example, MyMutableArray should be myMutableArray. RetrievingDataFromParse should be retrievingDataFromParse (and so on). Start upper case letter are for classes.
Second, what does this code mean (I put comment on your code)?
for (int i = 0; i < objects.count; i++) {
// where do you have defined object?
object = [objects objectAtIndex:i];
NSString *x = [object objectForKey:#"x"];
NSNumber *y = [object objectForKey:#"y"];
NSNumber *z = [object objectForKey:#"z"];
NSString *t = [object objectForKey:#"t"];
// is Mekan a subclass of PropertiyClass or what else?
PropertiyClass *Properties = [[Mekan alloc]initWithx:x y:y z:z t:t]
// what's MekanKalibi? Maybe you need to add Properties
[MyMutableArray addObject:MekanKalibi];
}
Edit
If you don't use iOS6 - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier you should alloc-init cells.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell) {
// alloc-init a new cell here...
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// or if you don't use ARC
// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
PropertyClass *PC = [myMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = PC.x;
return cell;
Edit 2
I don't know how parse works but I suppose it manages async requests. So, at the end of your for loop, just call reload data in the table.
Parse states:
The InBackground methods are asynchronous, so any code after this will run immediately. Any code that depends on the query result should be moved inside the completion block above.
I had the same problem. When you reload the table, you need to move it so it is inside the block. Worked for me.
I'm not 100% sure how the asynchronous parts affect it so. I know that the start of my viewDidload and the end occured then this block, hence the problem.
People should probably up this as this solves the issue.
Cheers.
All you have to do is reload tableView in the block... this will show data.
for (int i = 0; i < objects.count; i++) {
object = [objects objectAtIndex:i];
NSString *Propert1 = [object objectForKey:#"Propert1"];
NSNumber *Propert2 = [object objectForKey:#"Propert2"];
NSNumber *Propert3 = [object objectForKey:#"Propert3"];
NSString *Propert4 = [object objectForKey:#"Propert4"];
PropertiesClass *PC = [[PropertiesClass alloc]initWithPropert1:Propert1 Propert2:Propert2 Propert3:Propert3 Propert4:Propert4];
[myNSMutableArray addObject:PC];
};
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
**[MyTableView reloadData];**
}];

multiple checkmarks from

Tutorial I am following: http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/
I have created a tableview with 16 cells. When I select a row, it will show checkmark on it.
But when I scroll the tableview, there is also a checkmark showing on another cell further down the list. This repeats for any cell selected.
#import "FlightChecklistViewController.h"
#interface FlightChecklistViewController ()
#end
#implementation FlightChecklistViewController
{
NSArray *tableData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize table data
tableData = [NSArray arrayWithObjects:#"Egg Benedict", #"Mushroom Risotto", #"Full Breakfast", #"Hamburger", #"Ham and Egg Sandwich", #"Creme Brelee", #"White Chocolate Donut", #"Starbucks Coffee", #"Vegetable Curry", #"Instant Noodle with Egg", #"Noodle with BBQ Pork", #"Japanese Noodle with Pork", #"Green Tea", #"Thai Shrimp Cake", #"Angry Birds Cake", #"Ham and Cheese Panini", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:#"You've selected a row" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
Any suggestions?
You need to store the information about the rows indexpaths, that were selected, somehow.
And populate your cell according to it.
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) NSMutableArray *selectedCells;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedCells = [NSMutableArray array];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *unifiedID = #"aCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
}
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
//if the indexPath was found among the selected ones, set the checkmark on the cell
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
//if a row gets selected, toggle checkmark
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
[self.selectedCells removeObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCells addObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}
#end
you will find the complete example code on github
The problem is that cells are reused. So, if you add a checkmark accessory view to a cell further up it'll appear again when the cell is reused further down. You should save which ones are checkmarked in an array somewhere that correlates to the rows of the table when you add/remove a checkmark. Then, when you give the table view a new cell you can determine whether or not it needs a checkmark and set that up.
I had the same issue recently with one of my apps, and I fixed it by doing this:
#property (nonatomic, strong) NSArray *list;
- (void)viewDidLoad
{
[super viewDidLoad];
self.list = [[NSArray alloc] initWithObjects:#"foo", #"bar", nil];
}
- (NSString *)SettingsPlist
{
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *PlistPath = [paths stringByAppendingPathComponent:#"Settings.plist"];
return PlistPath;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[self list] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *contentForThisRow = [[self list] objectAtIndex:[indexPath row]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[self SettingsPlist]];
NSString *row = [NSString stringWithFormat:#"%d",indexPath.row];
if([[dict objectForKey:row]isEqualToString:#"0"])
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:[self SettingsPlist]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *row = [NSString stringWithFormat:#"%d",indexPath.row];
if(cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *on = #"1";
[plist setObject:on forKey:row];
[plist writeToFile:[self SettingsPlist] atomically:YES];
}
else if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSString *off = #"0";
[plist setObject:off forKey:row];
[plist writeToFile:[self SettingsPlist] atomically:YES];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Implementing UIWebView on a custom UITableViewCell

I have this UIWebView inside a custom UITableViewCell that I've created.
The problem is, when I implement the UIWebView in "MyMain", it's working but when I scroll the text is readded again and again on over the cells.
When I implement the UIWebView in the custom cell class itself it's not showing it at all.
UITableViewCustomCell.h
#property (nonatomic, weak) IBOutlet UIWebView *wvMainText;
#property (nonatomic, strong) NSString *wvTitle;
#property (nonatomic, strong) NSString *wvPrice;
UITableViewCustomCell.m
#synthesize wvMainText = _wvMainText;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// THIS IS WHERE I IMPLEMENT THE WEBVIEW?
}
return self;
}
MyMain
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myListCell";
UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"UITableViewCustomCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (UITableViewCustomCell *)view;
}
}
}
cell.wvTitle = [[self.arrList objectAtIndex:indexPath.row] valueForKey:#"title"];
cell.wvPrice = [[self.arrList objectAtIndex:indexPath.row] valueForKey:#"price"];
wv.scrollView.scrollEnabled = NO;
NSString *htmlString = [NSString stringWithFormat:
#"<html><body>%#,%#</body></html>", self.wvTitle, self.wvPrice];
[wv loadHTMLString:htmlString baseURL:nil];
[wv setBackgroundColor:[UIColor clearColor]];
[wv setOpaque:NO];
return cell;
}
Try this...It may solve your issue.
In your cellForRowAtIndexPath method please do changes like this....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myListCell";
UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"UITableViewCustomCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (UITableViewCustomCell *)view;
}
}
cell.wvTitle = [[self.arrList objectAtIndex:indexPath.row] valueForKey:#"title"];
cell.wvPrice = [[self.arrList objectAtIndex:indexPath.row] valueForKey:#"price"];
wv.scrollView.scrollEnabled = NO;
NSString *htmlString = [NSString stringWithFormat:
#"<html><body>%#,%#</body></html>", self.wvTitle, self.wvPrice];
[wv loadHTMLString:htmlString baseURL:nil];
[wv setBackgroundColor:[UIColor clearColor]];
[wv setOpaque:NO];
}
return cell;
}

customising UITableViewCell with picture objective-C

I want to have custom cell in UITableView, I create my UIView via storyboard and I linked them to the code
I don't know why my picture does not appear
Would you please check my code ?
customise code .h
: UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *weekImg;
#end
customise code .m
#synthesize weekImg;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
weekImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"red.png"]];
}
return self;
}
My method: cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"WeekTableViewCell"];
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
[[cell textLabel] setText:contentForThisRow];
return cell;
}
I think this is because your code is just looking for previosuly created cells. at th start no cells will be created and wont be able to retrieve any this way.
WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"WeekTableViewCell"];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"WeekTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[WeekTableViewCell class]])
{
cell = (WeekTableViewCell *)currentObject;
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
[[cell textLabel] setText:contentForThisRow];
return cell;
this way create a new cell if there isnt a cell to re use
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.imageView.layer.cornerRadius=10.0f;
[cell.imageView.layer setMasksToBounds:YES];
cell.textLabel.text=[NSMUtableArray objectAtIndex:indexPath.row];
switch (indexPath.row)
{
case 0:
cell.imageView.image=[UIImage imageNamed:#"img1.png"];
break;
case 1:
cell.imageView.image=[UIImage imageNamed:#"img2.png"];
break;
case 2:
cell.imageView.image=[UIImage imageNamed:#"img3.png"];
break;
case 3:
cell.imageView.image=[UIImage imageNamed:#"img4.png"];
break;
default:
break;
}
}
cell.imageView.layer.cornerRadius=10.0f;
[cell.imageView.layer setMasksToBounds:YES];, it is used by QuartzCore.
The output of the cell like this, we can adjust the image size.