Working with multiple data-sources in uitableview

Source: Internet
Author: User

Using this post, I am going to explain "working with multiple data-sources in uitableview ".

Expected output of this tutorial is demonstrated by following images.

Step 1: load data into your arrays (if you are fetching from XML/Web do that, I have implemented static here)

-(Void) viewdidload
{
[Super viewdidload];
Self. arone = [nsarray arraywithobjects: @ "Sagar", @ "Amit", @ "nimit", @ "Paresh", @ "Rakesh", @ "Yogesh", nil];
Self. artwo = [nsarray arraywithobjects: @ "supreeth", @ "deepthy", @ "ankit", @ "Sandeep", @ "gomathy", nil];
[Self. tableview reloaddata];
}

Step 2: Implement coding in such a way that handles multiple data-sources. It means place conditional coding as following example

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath
{
Static nsstring * cellidentifier = @ "cell ";

Uitableviewcell * cell = [self. tableview dequeuereusablecellwithidentifier: cellidentifier];
If (cell = nil ){
Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier] autorelease];
}

Cell. textlabel. Text = [(self. btntapped. Selected )? Self. artwo: Self. arone objectatindex: indexpath. Row];

Return cell;
} Step 3: Add a connection to your button or whatever control that you are using.

-(Ibaction) btntitle :( ID) sender {
Self. btntapped. Selected =! Self. btntapped. selected;
[Self. tableview reloaddata];
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.