简体   繁体   English

如何对包含2个相似和1个不同控件的tableview的自定义单元格进行排序

[英]how to sort the custom cell of an tableview containing 2 similar and 1 different controls

i have made a table view using custom cell. 我已经使用自定义单元格制作了表格视图。 My custom cell contains one image view(for showing movie image) and 2 labels( 1 for showing name and 1 for time) and one button for sorting which i have taken in navigation control bar. 我的自定义单元格包含一个图像视图(用于显示电影图像)和2个标签(用于显示名称的1个标签和用于时间的1个标签)以及一个用于排序的按钮(我在导航控制栏中已进行了排序)。 Now i want that on click of button it should sort the cell according to movie name. 现在我希望在单击按钮时,它应该根据电影名称对单元格进行排序。

What i have done so far is i have taken an array to store the strings of the uicontrols like strng_name for movie name, strng_time for time and strng_imge for image. 到目前为止,我已经完成了一个数组来存储uicontrol的字符串,例如电影名称为strng_name,时间为strng_time,图像为strng_imge。 and to sort it on the basis of name of movie I have taken one NSDictionary and setting key as name of movie and object or value as a array of strings. 并根据电影名称对其进行排序,我选了一个NSDictionary,并将键设置为电影名称,将对象设置为对象名称,或者将值设置为字符串数组。 But when iam setting the value of cell components iam not finding any way to fetch value from dicitonary? 但是,当IAM设置单元格成分的值时,IAM没有找到任何方法来获取二分值吗?

Can anyone please help me? 谁能帮帮我吗? If iam going wrong then please tell me an alternative. 如果iam出错,请告诉我另一种方法。

Try sorting the DataSource Array of your UITableView. 尝试对UITableView的数据源数组进行排序。 To do so, add an Action-Method to your Button where you can perform the sorting algorithm. 为此,请将“操作方法”添加到“按钮”,您可以在其中执行排序算法。

Then don't forget to reload the TableView. 然后,别忘了重新加载TableView。

[tableView reloadData]; 

To retrieve values of NSDictionary use following method: 要检索NSDictionary值,请使用以下方法:

[dictionary valueForKey:@"strng_name"];

If I understand it right this should give you an NSArray with the strings of your data 如果我理解正确,这应该为您提供一个包含数据字符串的NSArray

You need to sort your data source using a method when you click a button 单击按钮时,需要使用一种方法对数据源进行排序

id customSort = ^(NSString * key1, NSString * key2){
    return [key1 compare:key2];
};

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:customSort];

And then reload data 然后重新加载数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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