property not found on object UiTableViewCell - ios

i'm trying to show fetch data from core data into my tableview the problem is i get the.
´Property yttitle not found on object of type UITableView´
i've made sure the cell identifier is correct and the yttitle and the other are synthesized correctly. Why am i getting this error?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
// Configure the cell...
NSManagedObject *device = [devices objectAtIndex:indexPath.row];
cell.ytTitle.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"songName"]];
cell.ytAuthor.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"author"]];
[cell.ytThumbnail.layer setBorderColor: [[UIColor grayColor] CGColor]];
[cell.ytThumbnail.layer setBorderWidth: 1.0];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"%#", [device valueForKey: #"link"]]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.ytThumbnail.image = image;
return cell;
}

UITableViewCell doesn't have that properties, you have to create your own custom cell view and load it, you can use this code to load it from an xib
NSArray *xibObj = [[NSBundle mainBundle] loadNibNamed:#"CustomTVCell" owner:nil options:nil];
for(id currentObj in xibObj){
if ([currentObj isKindOfClass:[CustomTVCell class]]) {
cell = (CustomTVCell *) currentObj;
}
}

Probably If I understand what you want to do, creating a custom cell is not necessary. You can simply use the UITableViewCell with UITableViewCellStyleSubtitle and use the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
// Configure the cell...
NSManagedObject *device = [devices objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"songName"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"author"]];
[cell.imageView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[cell.imageView.layer setBorderWidth: 1.0];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"%#", [device valueForKey: #"link"]]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = image;
return cell;
}

It's complaining because you try to call this property (yttitle) on UITableViewCell object and there isn't any property like that. You should cast UITableViewCell to your custom class like that:
YOURCUSTOMTableViewCell *cell = (YOURCUSTOMTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[YOURCUSTOMTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
And make sure your YOURCUSTOMTableViewCell contain the property (yttitle, etc).
Hope this help.

Are you using custom cell for this TableView? Note that your method is return and using standard UITableViewCell, and this standard cell don't have a #propoerties like ytTitle and ytAuthor.
So if you're using custom cell you have to change it in three places in your code:
- (MyCustomCellClass *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[MyCustomCellClass alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
...
If you declare properties in your view controller (this don't make sense) you should move it to custom cell and get rid of it in VC.

Related

UItableView deplicated cell when scrolling

Sorry for posting this question again but I've looked into many answers and neither of them was helpfull to solve my issue.
So this my code :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"radioCell";
RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[RadioTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
[self configureCommentCell:cell atIndexPath:indexPath];
return cell;
}
when I scroll down my cell get mixed up and some of data are repeated, so I've tried this :
static NSString *CellIdentifier = #"memberCell";
RadioCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[RadioTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
and this :
RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[RadioTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
}
But it didn't fixed my issue and I get white empty cells ? please how to fix this issue ?
Update
- (void)configureCommentCell:(RadioTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSDictionary *object;
if ([_dataArray[indexPath.section] isKindOfClass:[NSArray class]])
object = [_dataArray[indexPath.section] objectAtIndex:indexPath.row];
else
object = [[_dataArray[indexPath.section] valueForKey:#"radioList"] objectAtIndex:indexPath.row];
if (object[#"jsonUrl"]) {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:object[#"jsonUrl"] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSDictionary *tempObject = (NSDictionary *) responseObject;
if (![[responseObject objectForKey:#"type"] isEqualToString:#"error"]) {
NSDictionary *tempObject = [responseObject[#"data"] objectAtIndex:0];
cell.playingNow.text = tempObject[#"song"];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
cell.name.text = [NSString stringWithFormat:#" %#", object[#"title"]];
if (object[#"logoUrl"])
[cell.logo setImageWithURL:[NSURL URLWithString:object[#"logoUrl"]]];
}
I see that your problem is that you are fetching the data of you cells inside configureCommentCell that's called inside cellForRowAtIndexPath. which is wrong, because it too late to fetch data inside cellForRowAtIndexPath, in this delegate method you should return the cell.
this line may be called before retrieving the data from server :
cell.name.text = [NSString stringWithFormat:#" %#", object[#"title"]];
Instead you should:
Fetch the data inside a separate method for example fetchData
when the data is downloaded inside the completion block of AFNetworking method, store the data inside an NSArray called for example myDataArray still inside the completion block call [self.tableView reloadData];
In viewDidLoad method just call your method fetchData
And your cellForRowAtIndexPath should looks like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// hey please give me the cell to display ... harry up please
// please harry up ! oh my god you are fetching data from server
// while I am asking for the cell !
// ok I don't care do what you want
// I will return an empty cell anyway
// and guess what I will not take in consideration
// the retried data because it's inside a block
// which is called asynchronously
static NSString *cellIdentifier = #"radioCell";
RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) {
cell = [[RadioTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; }
// now before return the cell you need to update the content of cell
// maybe you have an array of items and you should update the label
// for example here and then return the cell
cell.usernameLabel = self.myDataArray[indexPath.row]; // example
return cell;
}
Well the TableView is reusing the cells, and you add the image every time a cell is displaid. Thus when reusing the cell you add an other image, but there already is an image.
You will have to reuse the image view, and only add the image if you create the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifer = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer]autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20,0,30,44)];
imageView.tag = 1001;
[cell addSubview:imageView];
[imageView release], imageView= nil;
}
TabBarTestAppDelegate *delegate = (TabBarTestAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *local = delegate.myData;
// ok, it's horrible, don't look at it :-)
cell.textLabel.text = [NSString stringWithFormat:#"%#%#", #" " ,[local objectAtIndex:indexPath.row]];
//
NSString* name = nil;;
if (indexPath.row == 0) {
name = #"topicon";
}
else if (indexPath.row + 1 == [local count]) {
name = #"bottomicon";
}
else {
name = #"innericon";
}
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1001];
imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:name ofType:#"png"]];
return cell;
}

UITableView inside UIView - data loads only when scrolling cells outside uitableview

I have a problem using a UITableView inside a UIViewController:
When I start the app the UITableView is shown, but the data from _opponents isn't shown.
When I start scrolling the first cell outside the UITableView, now this cell gets updated.
Also, when I trigger [_topponents reloaddata] manually, the whole UITableView shows the correct data.
What do I have to add or change, so that the data is shown from the beginning?
dataSource and delegate are both connected with the class in the storyboard
Here is my code:
viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
myappdel = (AppDelegate *)[UIApplication sharedApplication].delegate;
self.title = #"New Match";
_lopponent.text = _opponent;
opponents = [[NSMutableArray alloc] init];
NSMutableDictionary *opponent = [[NSMutableDictionary alloc] init];
[opponent setObject:#"opponent A" forKey:#"name"];
[opponent setObject:#"xx matches | xx wins || xx losts" forKey:#"stats"];
[opponent setObject:#"Krems" forKey:#"location"];
NSMutableDictionary *opponent2 = [[NSMutableDictionary alloc] init];
[opponent2 setObject:#"opponent B" forKey:#"name"];
[opponent2 setObject:#"yy matches | yy wins || yy losts" forKey:#"stats"];
[opponent2 setObject:#"Location B" forKey:#"location"];
NSMutableDictionary *opponent3 = [[NSMutableDictionary alloc] init];
[opponent3 setObject:#"opponent C" forKey:#"name"];
[opponent3 setObject:#"zz matches | zz wins || zz losts" forKey:#"stats"];
[opponent3 setObject:#"Location C" forKey:#"location"];
[opponents addObject:opponent];
[opponents addObject:opponent2];
[opponents addObject:opponent3];
[_topponents reloadData];
}
and here my cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"opponentcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lopponentname = (UILabel*)[cell viewWithTag:1];
UILabel *lopponentstats = (UILabel*)[cell viewWithTag:2];
UILabel *llocation = (UILabel*)[cell viewWithTag:3];
lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"name"];
lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"stats"];
llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"location"];
return cell;
}
I found my fault!
I used to write:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
but I should use
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
with "forIndexPath"
I think, the trouble is in the cell. Create UITableViewCell subclass.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"opponentcell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"stats"];
cell.llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"location"];
return cell;
}
Hope it helps you.
Add this after initialisation of cell. For me works properly.
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
I think you are using custom cel and it looks you have trouble in "cellForRowAtIndexPath". If you using .xib, you may find it useful.
static NSString *cellIdentifier = #"opponentcell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibObjects =[[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:nil options:nil];
for (id currentObject in nibObjects)
{
if ([currentObject isKindOfClass:[CustomTableViewCell class]])
{
cell = (CustomTableViewCell *) currentObject;
}
}
[cell initViewStyles];
}
You need to check for condition when cell is nil..
Initially the cell is nil..
So the data is loading only after scrolling...
Add below lines of code in your cellForRowAtIndexPath delegate method after initializing cell.
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Hope this fixes the issue...

UIImage is showing on simulator, not on a real device

I have tried all of the suggestions in other similarly titled questions. My case seems to be special. Here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if ([menu_List count] == 0){
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSString *newString = [[MainMenuArray objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:#" " withString:#""];
UIImage *image1 = [UIImage imageNamed:[NSString stringWithFormat:#"%#%#", newString,#".png"]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image1];
[imageView setFrame:CGRectMake(5.0, 1.0, 41.0, 41.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[cell addSubview:imageView];
cell.textLabel.textColor = [UIColor whiteColor];
GetData *GD = [[GetData alloc] init];
cell.backgroundColor = [GD colorWithHexString:#"18204a"];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [MainMenuArray objectAtIndex:indexPath.row]];
}else{
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", [menu_List objectAtIndex:indexPath.row]];
cell.textLabel.textColor = [UIColor whiteColor];
GetData *GD = [[GetData alloc] init];
cell.backgroundColor = [GD colorWithHexString:#"18204a"];
}
return cell;
}
So the string name is correct, and it matches the case. I've checked my build settings, and the images are in copy bundle resources. All show up in the simulator. Only two of the seven appear in the list. If I change the order in which they appear, it is still the same two images that show up. They all show up in the simulator. I am using ECSlidingViewController to create a menu and I want the images to show up. Any idea on what could be causing it?
Clean your project and check for upper case letters. Simulator is not case sensitive, but real devices does not load an image if you use "myimage.png" on your code but your image name is MyImage.png
Make sure your png is enabled for the schema

Not able to view image in tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
cell.detailTextLabel.text = [[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];
cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
return cell;
}
Change your cell's style to UITableViewCellStyleDefault
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
If you want two labels in the cell, then add it to the contentView of the cell.
- (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];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
UIlabel *detailLbl = [[UILabel alloc]initWithFrame:CGRectMake(60,10,200,50)];
[detailLbl setText:[[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];];
[cell.contentView addSubView:detailLbl];
cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
return cell;
}
Try this code
- (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];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
cell.detailTextLabel.text = [[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];
//cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
//write this code
// Load the image with an GCD block executed in another thread
dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(downloadQueue, ^{
UIImage * image = [UIImage imageNamed:[temp objectForKey:#"category"]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
[cell setNeedsLayout];
});
});
dispatch_release(downloadQueue);
return cell;
}

Use a custom UITableViewCell for many UITableView

I have created a custom UITableViewCell for a UITableView by dragging and dropping the labels and imageView from my Prototype Cell to a Custom Cell which I call QuizInfoCell.
That works fine for the particular TableView that I created the Prototype Cell in, but I can't get QuizInfoCell to work for any other TableView. How can I achieve that?
This is the code that I'm using in the TableView that created the Prototype Cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuizInfoCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"QuizInfoCell" forIndexPath:indexPath];
if (cell == nil){
cell = [[QuizInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"QuizInfoCell"];
}
cell.name.text = [title objectAtIndex:indexPath.row];
cell.author.text = [author objectAtIndex:indexPath.row];
cell.time.text = [[ValueHandler alloc] getDateString:[timeStamp objectAtIndex:indexPath.row]];
cell.plays.text = [#"Plays: " stringByAppendingString:[NSString stringWithFormat:#"%#", [plays objectAtIndex:indexPath.row ]]];
cell.ratingUps.text = [NSString stringWithFormat:#"%#", [ratingUps objectAtIndex:indexPath.row]];
cell.ratingDowns.text = [NSString stringWithFormat:#"%#", [ratingDowns objectAtIndex:indexPath.row]];
cell.qid.text = [NSString stringWithFormat:#"%#", [qid objectAtIndex:indexPath.row]];
return cell;
}
I suggest creating your QuizInfoCell as a new XIB file.
Press CMD + N
Select the User Interface submenu.
Create a new View and call it QuizInfoCell
In your new XIB delete the existing View element and instead drag and drop a new Table View Cell.
Recreate your custom UITableViewCell in the same way as you did before by dragging and dropping the labels and imageView
Finally give the new Cell an Identifier called QuizInfoCell, like this:
You can then load your new Custom UITableViewCell with Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuizInfoCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"QuizInfoCell" forIndexPath:indexPath];
if (cell == nil){
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"QuizInfoCell" owner:self options:nil];
cell = [nibContents objectAtIndex:0];
}
cell.name.text = [title objectAtIndex:indexPath.row];
cell.author.text = [author objectAtIndex:indexPath.row];
cell.time.text = [[ValueHandler alloc] getDateString:[timeStamp objectAtIndex:indexPath.row]];
cell.plays.text = [#"Plays: " stringByAppendingString:[NSString stringWithFormat:#"%#", [plays objectAtIndex:indexPath.row ]]];
cell.ratingUps.text = [NSString stringWithFormat:#"%#", [ratingUps objectAtIndex:indexPath.row]];
cell.ratingDowns.text = [NSString stringWithFormat:#"%#", [ratingDowns objectAtIndex:indexPath.row]];
cell.qid.text = [NSString stringWithFormat:#"%#", [qid objectAtIndex:indexPath.row]];
return cell;
}

Resources