Divank Kumawat Divank Kumawat - 2 months ago 46
Swift Question

How to create news feed UI in swift

I have create a news feed app. I get a news data from server. In my data news available on text OR image with text(same as Facebook feed).
I have successfully create before in UITableviewCell images with text content, but I am trying to many time in UITableViewCell text or images with text, Both multiple cell first cell is only text and second cell is image with text. How to create, I know i am not explain proper way but i want to create a same as Facebook feed(TimeLine).

And Which type of array or dictionary create this view. some cells in text view and some cells image and text view. How to create dynamic cell. I am so confused. How to create news feed ui and in which type of array create this news feed. Please help. I am new in IOS. Thank you.

Please find the attachment and see ones. I want to like this.

Only text view
enter image description here

Image and text view

enter image description here


Add a UILabel and UIImageView as you normally would in a cell. (I would suggest a UILabel instead of a UITextView if you are only displaying the text, correct me if I'm wrong). Setup your constraints for the label to have a leading, top and trailing. Set its numberOfLines to be 0 and content mode to word wrap. Now for your UIImageView, add a top to the UILabel. Leading, trailing and bottom to superView. Now also setup a height constraint for your UIImageView that will be a minimum value. Create an IBOutlet for this constraint inside your cell.

Now, in your UIViewController set the tableView.estimatedRowHeight property and also tableView.rowHeight = UITableViewAutomaticDimensions. Soo now, inside your cellForRowAtIndexPath, whenever your cell contains an image set the height constraint to be the minimum value otherwise set it to 0.


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   CustomCell *cell = [tableView dequeueReusableCellWithReuseIdentifier:@"cell"];

  //select your model from the array corresponding to your indexPath.row
  CustomModel *model = array[indexPath.row]

  // I am assuming you are fetching image from server
  NSString *imageUrlString = model.imageUrl

  //check your model if it contains an image
      cell.imageHeightConstraint = 250.0f;
      cell.imageHeightConstraint = 0.0f;