IOS Study Notes (9) tableview Basics

Source: Internet
Author: User

Tableview is a rolling view divided into different parts. Each part is further divided into rows. Each row is an instance of the uitableviewcell class. You can embed images, text, and anything into tableview cells to customize their shapes, heights, groups, or more. These are defined in the uitableviewdatasource and uitableviewdelegate Protocols respectively.

# Import <uikit/uikit. h>

@ Interface tableviewcontroller: uiviewcontroller

@ Property (monatomic, strong) uitableview * mytableview;

@ End

@ Implementation tableviewcontroller

@ Synthesize mytableview;

-(Void) viewdidload {

[Super viewdidload];

Self. mytableview = [[uitableview alloc] initwithframe: Self. View. bounds style: uitableviewstyleplain];

[Self. View addsubview: Self. mytableview];

}

In this way, a blank tableview is created.

Uitableviewstyleplain
Create a blank table view with no background image
Uitableviewstylegrouped
Create a table view with a border of a background image and a rounded corner, similar to the Settings app.

How to add data?

Follow the uitableviewdelegate protocol:

// Set the height of tableviewcell

-(Void) viewdidload {

[Super viewdidload];

Cgrect tableviewframe = self. View. bounds;

Self. mytableview = [[uitableview alloc] initwithframe: tableviewframe style: uitableviewstyleplain];

Self. mytableview. Delegate = self;

[Self. View addsubview: Self. mytableview];

}

-(Void) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath {

Cgfloat result = 20366f;

If ([tableview isequal: Self. mytableview]) {

Result = 40366f;

}

Return result;

}

The positions of cells in tableview are displayed by their index paths. An index path is a combination of section and row indexes. The Section index starts from scratch.

Add data:

To add data, follow the uitableviewdatasource protocol.

Add code

Self. mytableview. datasource = self;

Self. mytableview. autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight;

-(Nsinteger) numberofsectionsintableview :( uitableview *) tableview {

Nsinteger result = 0;

If ([tableview isequal: Self. mytableview]) {

Result = 3;

}

Return result;

}

-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {

Nsinteger result = 0;

If ([tableview isequal: Self. mytableview]) {

Switch (section ){

Case 0 :{

Result = 3;

Break;

}

Case 1 :{

Result = 5;

Break;

}

Case 2 :{

Result = 8;

Break;

}

}

}

Return result;

}

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforatindexpath :( nsindexpath *) indexpath {

Uitableviewcell * result = nil;

If ([tableview isequal: Self. mytableview]) {

Static nsstring * tableviewcellidentifier = @ "mycells ";

Result = [tableview dequeuereusablecellwithidentifier: tableviewcellidentifier];

If (result = nil ){

Result = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: tableviewcellidentifier];

}

Result. textlabel. Text = [nsstring stringwithformat: @ "section % lD, cell % lD", (long) indexpath. Section, (long) indexpath. Row];

}

Return result;

}

Receive and process table view events

-(Void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {

If ([tableview isequal: Self. mytableview]) {

Nslog (@ "% @", [nsstring stringwithformat: @ "cell % LD in section % LD is selected", (long) indexpath. row, (long) indexpath. section]);

}

}

Use different types of attachments in tableview

Result. accessorytype = uitableviewcellaccessorydetaildisclosurebutton;

Create a custom tableview cell attachment

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {

Uitableviewcell * result = nil;

Static nsstring * mycellidentifier = @ "simplecell ";

Result = [tableview dequeuereusablecellwithidentifier: mycellidentifier];

If (result = nil ){

Result = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: mycellidentifier];

}

Result. textlabel. Text = [nsstring stringwithformat: @ "section % lD, cell % lD", (long) indexpath. Section, (long) indexpath. Row];

Uibutton * button = [uibutton buttonwithtype: uibuttonwithtype: uibuttontyperoundedrect];

Button. Frame = cgrectmake (0.0f, 0.0f, 150366f, 25366f );

[Button settitle: @ "Expand" forstate: uicontrolstatenormal];

[Button addtarget: Self action: @ selector (performexpand :) forcontrolevents: uicontroleventtouchupinside];

Result. accessoryview = button;

Return result;

}

-(Void) performexpand :( ID) paramsender {

/* Take an at ion here */

Uitableviewcell * ownercell = (uitableviewcell *) paramsender: superview;

If (ownercell! = Nil ){

Nsindexpath * ownercellindexpath = [self. mytableview indexpathforcell: ownercell];

Nslog (@ "accessory in index path is tapped. indexpath = % @", ownercellindexpath );

If (ownercellindexpath. Section = 0 & ownercellindexpath. Row = 1 ){

/* This is the second row in the first section */

}

}

}

Related Article

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.