简体   繁体   中英

How to add variable number buttons in uitableview cell

I am new to iOS development. In my app i'm using tableview for displaying data. When getting data from server each data for the day needs to be shown in a single cell. Each data needs be shown on button which can be clicked to download the data. Example. For today there are 3 data objects, So 3 buttons need be created inside a single cell. For yesterday there are 5 data objects, so 5 buttons are needed to be inside the single cell. Also the row height should vary accordingly. How to achieve it?

And also how to use the added buttons effectively.

Thank you

For set row height according to it's content you should set tableview's two properties

_tableViewOne.estimatedRowHeight = 50; //minimum row height
_tableViewOne.rowHeight = UITableViewAutomaticDimension;

so it will take row height according to it's content.

Don't use tableview's heightForRowAtIndexPath method otherwise above code will not work (it will override those properties).

and for dynamically add buttons you can create them and add subview them in cell at runtime in cellForRowAtIndexpath method.

If u want every button's user interaction then u can set every button's tag property according to indexpath.row and than call same mathod when any those button get clicked and then u can uniquely identify those buttons from there tag.

Example code:

-(void)buttonAction:(UIButton*)sender{
    switch (sender.tag) {
        case 1:
            //do something
            break;
        case 2:
            //do something
            break;
        case 3:
            //do something
            break;
        case 4:
            //do something
            break;
        default:
            break;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM