【发布时间】:2014-11-25 10:25:18
【问题描述】:
我正在使用 Xamarin 开发一个小型测试 iOS 应用程序,该应用程序显示一个包含推文的 TableView。我有一个 Starter Edition 帐户,因此我避免使用组件,因为它们会增加我的二进制文件的大小。
我有一个 TableViewController 并用足够的数据填充 UITableView 以生成滚动,但它不会滚动。
我已在 Storyboard 上将 Scroll Enabled 设置为 true,并且我的 TableSource 类有一个包含要显示的数据的 List。 UITableView 中的每个单元格(我使用的单元格样式是 UITableViewCellStyle.Value1)都会显示推文文本、发布者、创建日期和用户图像。
我的 TableSource 类的结构如下:
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;
}
}
如您所见,我有一个包含 Tweet 对象的列表(一条推文包含文本、用户名、一个 DateTime 对象和用户图像 url),我还有一个字典来保存下载的用户照片。单元格的创建没有问题,并且看起来应有的样子。
但 UITableView 不会滚动。
解析推文并创建 UITableView 源的方法是这样的:
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);
});
}
}
}
我在CollectionViews上也遇到过类似的问题,控件里面的内容足以引起滚动但是没有触发滚动。
我错过了什么吗?为什么不滚动内容?
编辑 1
为了确保 UITableView 确实有超出可见框架的单元格,我调用了 ScrollRectToVisible 方法,并且 UITableView 滚动到指定区域,通过编程我可以滚动但 UITableView 忽略了用户触摸手势。
是否必须指定触摸手势才能启用滚动?当我点击一行时它会被选中,那么为什么只有滚动没有响应?
编辑 2
我忘了澄清一点,UITableView 是通过带有 TableViewController 的 Storyboard 生成的。
【问题讨论】:
标签: ios uitableview xamarin.ios xamarin xamarin-studio