تخصيص TableView تعتمد على البيانات
-
20-09-2019 - |
سؤال
ولدي tableview مجمعة التي يتم ملؤها بالبيانات XML في مقطع واحد. ما أود القيام به هو إنشاء قسم آخر قبل البيانات مدفوعة واحدة، وتطبيق الإجراءات اللازمة لذلك.
مثال:
وتقدموللمستخدم مع الزر الذي يقول "استخدام موقعك الحالي" (تم إنشاؤه يدويا قسم) وتحت هذا هو قائمة البلدان يمكن للمستخدم اختيار بدلا من اختيار من بينها (بيانات مدفوعة القسم)
استخدم قائمة الإعدادات كدليل. هناك بعض الخيارات التي هي صف واحد في القسم، ولذلك يبدو أن هناك زر ...
إذا هذا لا معنى له، وسأحاول أن أشرح ذلك أفضل.
ولذا فإن لدي هذه الخطوط واضحة اثنين من التعليمات البرمجية ... يكفي بسيطة
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [countrysData count];
}
ما أود هو أن يكون numberOfSectionsInTableView
عودة 2 ويكون أول "القسم" ويقول "انقر لاستخدام موقعك الحالي" الذي من شأنه ثم دفع إلى عرض الخريطة، والقسم الثاني عرض لائحة الدول لدي حاليا العمل.
المحلول
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0){
return 1;
}else{
return [countrysData count];
}
}
اقتباس فقرة>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
وبعد ذلك يجب عليك اختيار ما يجب القيام به في didSelectRowAtIndexPath الخاص بك: طريقة بسبب indexPath.section. أوه، ويجب أن تحقق indexPath.section في cellForRowAtIndexPath الخاص بك: الأسلوب.
نصائح أخرى
وتحتاج فقط لتحديث كافة التطبيقات الخاصة بك من البروتوكولات UITableViewDataSource وUITableViewDelegate إلى نحو ملائم حساب للقسم الجديد وخلافها (ق).
وعلى سبيل المثال، وهنا كيفية تحديث <لأ href = "http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewDatasource_Protocol/Reference/Reference.html#//apple_ref/doc/uid / TP40006941-CH3-SW9 "يختلط =" noreferrer نوفولو "> numberOfSectionsInTableView: و <لأ href =" http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewDatasource_Protocol/Reference/ Reference.html # // apple_ref / وثيقة / رمز المستخدم / TP40006941-CH3-SW8 "يختلط =" noreferrer نوفولو "> tableView: numberOfRowsInSection: ، و <لأ href =" http://developer.apple.com/ اي فون / مكتبة / وثائق / UIKit / المرجع / UITableViewDatasource_Protocol / المرجع / Reference.html # // apple_ref / وثيقة / رمز المستخدم / TP40006941-CH3-SW6 "يختلط =" noreferrer نوفولو "> tableView: cellForRowAtIndexPath: ، ولكنك سوف تحتاج إلى تحديث على الأقل <لأ href = "http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006942-CH3 -SW14 "يختلط =" noreferrer نوفولو "> ر ableView: didSelectRowAtIndexPath: أيضا:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// calculate the number of sections of non-data (might just be 1)
// calculate the number of sections for the data (you were already doing this, might just be 1)
// return the sum
return 1 + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rowCount = 0;
switch (section) {
case 0:
// non-data section has 1 row/cell
rowCount = 1;
break;
case 1:
// data section uses an array
rowCount = [dataArray count];
break;
}
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *nonDataCellID = @"NonDataCell";
static NSString *dataCellID = @"DataCell";
UITableViewCell *cell;
int section = [indexPath indexAtPosition:0];
int row = [indexPath indexAtPosition:1];
switch (section) {
case 0:
// or you can just use standard cells here
cell = [tableView dequeueReusableCellWithIdentifier:nonDataCellID];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"NonDataCell" owner:self options:NULL];
cell = nonDataCell; // nonDataCell is an IBOutlet to this custom cell
}
// configure non-data cell here (use tags)
UILabel *someLabel = (UILabel*)[cell viewWithTag:1];
someLabel.text = @"Non-data cell";
break;
case 1:
// or you can just use standard cells here
cell = [tableView dequeueReusableCellWithIdentifier:dataCellID];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"dataCell" owner:self options:NULL];
cell = dataCell; // dataCell is an IBOutlet to this custom cell
}
// configure data call here (using "row")
UILabel *someDataLabel = (UILabel*)[cell viewWithTag:1];
someDataLabel.text = [[dataArray objectAtIndex:row] valueForKey:@"name"];
break;
}
return cell;
}
وأنا جدا متأكد من أنك يمكن أن يغير عودة أسلوب UITableViewDataSource في "numberOfSectionsInTableView:" على الطاير. وبمجرد أن يختار المستخدم اختيار فرع إضافي، فقط تعيين إشارة لطريقة إرجاع عدد من الجداول التي تريدها. ثم قمت فرض إعادة تحميل من على الطاولة وسترى قسم جديد.