Drawing Game Center like tableview cells

后端 未结 2 542
再見小時候
再見小時候 2021-02-10 16:40

I\'ve now added a bounty which will be awarded to anyone who can take the three images below and produce a working implementation of a UITableView that mimicks the l

2条回答
  •  太阳男子
    2021-02-10 17:07

    I'm not sure to answer exactly what you asked for because I used a grouped tableView, but when testing it seems to do the trick, and the background scrolls with the cells, as it can be seen in game center.

    By the way, sorry, I don't use objective C but Monotouch and C#, but I know from experience it's not so hard to find bindings from one to the other when finding an interesting trick written in another language.

    Well, stop babbling, here's the code.

    public class test : UIScrollView {
        UIImage _borderTexture = null;
        int _paddingTop = 10;
    
        public test (RectangleF frame) : base (frame) {
            //setting the green background for the whole view
            BackgroundColor = UIColor.FromPatternImage(new UIImage("bg.png")); 
            //initializing texture for borders
            _borderTexture = new UIImage("bt.png");
    
            //adding some various-sized tables
            addTable(new string[] {"lonely cell"});
            addTable(new string[] {"top cell", "middle cell 1", "middle cell 2", "bottom cell"});
            addTable(new string[] {"this", "is", "a", "quite", "long", "table", "for", "an", "exemple"});
    
            //and then we adjust the scroll size to contents
            ContentSize = new SizeF(frame.Size.Width, _paddingTop);
        }
    
        void addTable(string[] contents) {
            // approximately keeping track of content's heights to adjust scrollView size
            // the table must be AT LEAST as high as its content to avoid its own scroll
            int approximativeHeight = contents.Length * 44 + 25;
            UITableView t = new UITableView(new RectangleF(10, _paddingTop, Frame.Width - 20, approximativeHeight), UITableViewStyle.Grouped);
            _paddingTop += approximativeHeight;
    
            //make the tableView's background invisible
            t.BackgroundColor = UIColor.FromWhiteAlpha(0, 0);
            //setting the yellow texture for cell borders
            t.SeparatorColor = UIColor.FromPatternImage(_borderTexture);
            //using our own data source to customize cells at creation
            t.DataSource = new testTVDataSource(contents);
    
            //finally, add the custom table to the scroll view
            AddSubview(t);
        }
    }
    
    public class testTVDataSource : UITableViewDataSource {     
        string[]    _contents = null;
        string      _cellID = "reuse";
    
        public testTVDataSource(string[] contents) {
            _contents = contents;
        }
    
        public override int RowsInSection (UITableView tableView, int section) {
            return _contents.Length;
        }
    
        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) {
            var cell = tableView.DequeueReusableCell (_cellID);
            if (cell == null) {
                cell = new UITableViewCell (UITableViewCellStyle.Default, _cellID);
                //make all backgrounds of the cell and its subviews to be invisible
                cell.BackgroundColor = UIColor.FromWhiteAlpha(0, 0);
                cell.TextLabel.BackgroundColor = UIColor.FromWhiteAlpha(0, 0);
                //avoid the blue highlighting when selected
                cell.SelectionStyle = UITableViewCellSelectionStyle.None;
            }
            //just some content for test
            cell.TextLabel.Text = _contents[indexPath.Row];
            return cell;
        }
    }
    

    So the main tweak resides in making your tableView transparent and high enough for it to not need scrolling, and to stick it into a scrollView which will scroll its background with its content (here, the tableViews.)

提交回复
热议问题