MGTwitterEngine and TableView - iphone

I sorta understand better how what the MGTwitterEngine returns....I thought. But i'm still doing something wrong to get it into my table view. This is what I have in my cellforrowatindexpath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Default"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Default"] autorelease];
}
NSDictionary *record = [NSDictionary dictionaryWithContentsOfFile:[twitterEngine getUserTimelineFor:username since:nil count:20]];
cell.text = [record valueForKey:#"text"];
return cell;
}
What am I doing wrong? Thanks

getUserTimelineFor:since:count: does not return a list of tweets. It executes an asynchronous call that goes off and attempts to download the information. Whether or not this is successful is communicated to the object designated as the engine's delegate (and implements the MGTwitterEngineDelegateProtocol).
In other words, you need to understand the delegate pattern a bit more.

I was totally overlooking the delegate methods and what they can do. MGTwitterEngine is more powerful than I initially thought. What worked was using the did get statusRecieved method to create an array from the returned array.

Related

How to dynamically update your UITableView when a button is pressed?

I'm stuck with a seemingly very simple app that I'm trying to develop
Basically, I have a UITable, and two buttons: red/blue
When a button is pressed, a row with corresponding title of that button is append to the table
I'm overwhelmed by how complicated UITableView has to be implemented (datasource, delegate, resuable identifier, etc)
Can anyone help me out with this, preferably show me detailed codes
For my Buttons, I have something like this
- (IBAction)buttonPressed:(UIButton *)sender{
NSString *item = sender.currentTitle;
[self.cellArray addObject:item];
[self.myTable reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cellArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"The Table Cell";
self.myTableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (self.myTableCell == nil) {
self.myTableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
self.myTableCell.textLabel.text = [self.cellArray objectAtIndex:indexPath.row];
return self.myTableCell;
}
Since you are learning this..i will post a simple solution.
Make add a member variable NSMutableArray *cellArrays;
initialize it in your viewDidLoad
in buttonPressed check;
if([#"red" isEqualToString:[YourButton titleForState:UIControlStateNormal]])
{
[self.cellArrays addObject:#"red"];
}
else
{
[self.cellArrays addObject:#"blue"];
}
[self.YOURTABLEVIEW reloadData];
Now in your table view datasource method
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cellArrays.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//in between your code;
YOURCELL.textlabel.text = [self.cellArrays objectAtIndex : indexpath.row];
}
Try this ..
I'd recommend subclassing UITableViewController, then override the dataSource and delegate methods and you should be good to go. Going straight to UITableView is more complicated without any obvious benefits.
You may not be using Core Data, but I still recommend this lecture because it hooks up a TableViewController and works great - all code included. Check it out:
http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/289
The lectures in iTunes U explain everything further.
Enjoy,
Damien
I wanted to add that "One more thing, the compiler shows a warning says no reusable identifier. what exactly is that?" above...
His answer is correct, but to get rid of that compiler warning...
The tableviewcell of your tableview has to have an identifier. In IOS 5, in your storyboard, highlight the TableViewCell in your TableView, and enter a value in the Identifier. This must match the value in your code of the cell that you are creating.

Linking UITableView with a NSMutableArray

I've been trying to fill a UITableView with data from a NSMutableArray. I have a View-Based application project and so far I've tried every tutorial I found but so far, it just doesn't work. I linked the dataSource and delegate to the File's Owner, I added the procotols UITableViewDelegate and UITableViewDataSource to my UIViewController, I create an array containing 3 strings just to try and debug, everytime I load the app, the table stays empty.
I'm not sure what could be my error, or where I should look to find the problem, if you guys got any idea that would be nice.
Thank you
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection: (NSInteger)section
{
return [yourArray count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [yourArray objectAtIndex:row];
return cell;
}
That should populate the table from an array. I've used it several times in my app.
I guess you did forget to call -reloadData on the table view.
Make sure the dataSource and delegate are properly connected to the view controller, also make sure your UITableView is property connected with the outlet if you are using storyboards or a xib file.
If nothing works I recommend you to use XLData that makes UITableView data loading much more simple to implement.

UITableView scrolling crashes app

When i scroll UITableView it crashes the app. Here is code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *tempDict = [albums objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"ImageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
AsyncImageView *oldImage = (AsyncImageView *)[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
I set the breakpoints and it stops at line
NSDictionary *tempDict = [albums objectAtIndex:indexPath.row];
What am i doing wrong?
Thanks in advance.
I am not sure, first of all please check the total number of rows and your array count. They must be same. If still it crashes then put this line
NSDictionary *tempDict = [[albums objectAtIndex:indexPath.row] copy];
Check size of albums array. May be it is smaller then indexPath.row or it was released somewhere.
Make sure you are not reading out of bounds of your NSdictionary.
Check your return value in the tableView:numberOfRowsInSection: dataSource method.
The most likely reason for a crash on that line is that your table view has more rows than you have items in your albums array. Normally, in a setup like this, you'd have exactly the same number of rows in your table, and your implementation of –tableView:numberOfRowsInSection: would look like this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// assuming that there is only one section
return [albums count];
}
I solved this problem. Actually in the $cellForRow ,I was trying to get information from server, I did not apply lazy loading there. Because of that while scrolling the tableView, app was crashing.
Thank you to all for your help.

Is there a way in xcode 4 to list all delegate functions and automatically add the code snippet to a file?

This seems like an obvious feature that should be there but i can't find it.
For example if my class is a uitableviewdelegate whats the quickest way for me to see all the available delegate methods and add the ones im interested in to my implementation file?
You can use the tip in the first link that Simon posted above to get the method signatures for a delegate. After that, adding the code is up to you.
What I like to do is to choose the delegate methods that I most often use, and add them to a code snippet.
For example, for UITableViewDatasource I have a snippet called "UITableView Datasource Required Methods" containing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"myCellName";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
}
//customize cell before showing it
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#numRow#> ;
}
Then I drag that snippet into my code whenever I'm creating a table delegate.

why [self.tableView reloadData] does not Work?

This is the code for cellForRowAtIndexPath: method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [array objectAtIndex:row];
return cell;
}
When i use [self.tableView reloadData]; nothing happening?
You need to implement at least the tableView:numberOfRowsInSection: method as part of the UITableViewDataSource protocol, and tableView:cellForRowAtIndexPath: (which you already did). You should also implement numberOfSectionsInTableView:.
Then you need to make sure that your class is actually used as a data source:
self.tableView.dataSource = self;
That needs to be either done via Interface Builder or in awakeFromNib or some other method like viewDidLoad:.
Do you check your dataSource and delegate of UITableView ? I think the dataSource is nil or something else.
Try putting a break point in cell for row and see if it is called when u call reloadData. Also pls check if the changes are already made to array from which the cells are populated and make sure the table view is connected to its datasource and delegate.