ios - UITableViewDelegate and UITableViewDatasource confusion -
i understood before that
delegate
pattern used invoking events delegate instance
, getting controls (like size / font / etc...).
datasource
pattern getting data datasource instance
(like views / title / description / etc...)
but seems nice illusion, after looking apple's uitableviewdelegate protocol
got confused because
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath; - (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section; - (cgfloat)tableview:(uitableview *)tableview heightforfooterinsection:(nsinteger)section;
are delegate methods (but thinking uitableviewdatasource
methods)
is dirty code apple, or i'm missing important understand difference between datasource , delegate?
edit: @dbd nice answer, here more confusion
here uitableviewdelegate method returns view drawing
- (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section;
and there configuration in uitableviewdatasource
- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section;
and oops, can see method returns view in uitableviewdatasource
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath;
here have question why cellforrowatindexpath:
, viewforheaderinsection:
not in uitableviewdatasource
this how i've thought it.
uitableviewdatasource
primary data. actual contents of table. how many rows? content of row x?
uitableviewdelegate
secondary , display data. how tall should be, should display in selected state, , call backs "hey i'm something."
however admit see of fine line (and don't buy of choices)
uitableviewdatasource
has titleforheaderinsection
. uitableviewdelegate
has viewforheaderinsection
.
so if it's pure "data" title, it's data source, if includes display wrapper view, it's delegate. wait, cellforrowatindexpath
view , that's part of data source, why put viewforheaderinsection
in delegate? while can barely see distinction between "cell" data , "title view" delegate, think confusion of splitting "title" methods different protocols not preferable. i'm sure many might disagree me, it's opinion.
Comments
Post a Comment