简体   繁体   中英

Xamarin UITableView won't scroll

I'm using Xamarin to develop a small test iOS app that displays a TableView filled with tweets. I have a Starter Edition account so I'm avoiding the use of Components since those increase the size of my binaries.

I have a TableViewController and filled the UITableView with enough data to generate a scroll but it won't scroll.

I have set Scroll Enabled to true on the Storyboard and my TableSource class has a List that holds the data to be displayed. Each cell (the cell style I'm using is UITableViewCellStyle.Value1) in the UITableView displays the tweet text, who posted it, the creation date and the user image.

My TableSource class is structured like this:

public class TableSource : UITableViewSource
{
    List<Tweet> tableItems;
    public static string cellIdentifier = "TableCell";
    public Dictionary<string, UIImage> photoCache;

    public TableSource (List<Tweet> items)
    {
        tableItems = items;
        photoCache = new Dictionary<string, UIImage> ();
    }

    public override int RowsInSection(UITableView tableview, int section){
        return tableItems.Count;
    }

    public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath){
        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);

        if (cell != null) {
            cell = new UITableViewCell (UITableViewCellStyle.Value1, cellIdentifier);

        }

        var index = indexPath.Row;

        var tweet = tableItems [index];

        cell.TextLabel.Text = tweet.TweetText;
        cell.DetailTextLabel.Text = "by " + tweet.UserName + " at " + tweet.CreatedAt.ToShortDateString();

        if (!photoCache.ContainsKey(tweet.UserImage)) {
            using (var imgUrl = new NSUrl (tweet.UserImage)) {
                using (var data = NSData.FromUrl (imgUrl)) {
                    var img = UIImage.LoadFromData (data);
                    cell.ImageView.Image = img;
                    photoCache.Add (tweet.UserImage, img);
                }
            }
        } else {
            cell.ImageView.Image = photoCache [tweet.UserImage];
        }
        return cell;
    }
}

As you can see I have a List that holds Tweet objects (a tweet has text, user name, a DateTime object and user image url), also I have a Dictionary to hold the users photos that are downloaded. The cells are created with no problem and look as they should.

But the UITableView won't scroll.

The method that parses the tweets and creates the UITableView's source is this:

    private void ReadStreamFromTwitterMentionsResponse(WebResponse response){
        using (Stream responseStream = response.GetResponseStream ()) {
            using (StreamReader sr = new StreamReader (responseStream)) {
                // read response and parse it
                string content = sr.ReadToEnd ();
                var json = JsonObject.Parse (content);
                var statuses = json ["statuses"];
                // for each status create a Tweet object and put it in a List
                foreach (JsonObject status in statuses) {
                    try{
                        var user_name = status ["user"] ["name"];
                        var user_image = status ["user"] ["profile_image_url"];
                        var tweet_text = status ["text"];
                        var tweet_date = status ["created_at"];
                        Console.WriteLine("tweet_date " + tweet_date);
                        DateTime tweet_date_obj = DateTime.ParseExact (tweet_date, "ddd MMM dd HH:mm:ss zzzz yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal);

                        tweets.Add(new Tweet(tweet_text, user_name, user_image, tweet_date_obj));
                    }catch(Exception ex){
                        Console.WriteLine ("Error " + ex.Message);
                    }
                }
                // After finishing filling the List with tweets, tell main thread to
                // make a TableSource instance and pass the tweets list and to reload its data
                InvokeOnMainThread (delegate {
                    TableView.Source = new TableSource (tweets);
                    TableView.ReloadData();
                // The following line returns a Rectangle which height value is greater than the device's height
                Console.WriteLine("content size " + TableView.ContentSize);
                });
            }
        }
    }

I came across a similar problem on CollectionViews, the content inside the control is enough to provoke a scroll but the scrolling isn't triggered.

Am I missing something? Why isn't scrolling the contents?

EDIT 1

To make sure the UITableView did have cells beyond the visible frame I called ScrollRectToVisible method and the UITableView scrolled to the specified area, programatically I can scroll but the UITableView is ignoring the user touch gesture.

Do I have to specify a touch gesture to enable the scrolling? When I click on a row it will be selected, so how come only the scroll is not responding?

EDIT 2

I forgot to clarify something, the UITableView was generated through the Storyboard with a TableViewController.

This is most likely a bug in the iOS simulator. Sometimes is just stops detecting some gestures. Quit the simulator and restart it.

The UITableViewController should handle scrolling your table for you without needing to do anything special.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM