我正在制作一个聊天应用程序,其中在我的应用程序中显示消息有一些限制。如果用户不是订阅者,则在uitableview中他只会看到3条消息。我只想向用户显示3条消息,并且不限制发件人消息。我的表格视图中有2个单元格,一个用于发送者,第二个用于接收者。这是我的条件。
这显示默认值为空白的接收器单元。请指导我如何仅打印3条不会影响其他单元格的消息。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print(indexPath.section)
if indexPath.section == 0
{
let model = senderMessageArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! CellTwo
if (model.sender_id == AppData.user_id()!)
{
//SENDER
cell.backgroundColor = UIColor.clear
let lbl_send = cell.viewWithTag(11) as! UILabel
let lbl_date = cell.viewWithTag(12) as! UILabel
lbl_send.text = AppData.base64Decode(base64: model.message!)
lbl_date.text = AppData.TimeDateFormat(date: model.datetime!)
}
return cell
}
if indexPath.section == 1
{
//RECEIVER
let cellTwo = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ConversationTableViewCell
let model = receiveMessageArray[indexPath.row]
cellTwo.backgroundColor = UIColor.clear
let lbl_send =
cellTwo.viewWithTag(22) as! UILabel
let lbl_date = cellTwo.viewWithTag(23) as! UILabel
lbl_send.text = AppData.base64Decode(base64: model.message!)
lbl_date.text = AppData.TimeDateFormat(date: model.datetime!)
return cellTwo
}
else
{
return UITableViewCell()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
print("sections: \(section)")
print("receive Messages Count : \(receiveMessageArray.count)")
print("senderMessageArray Messages Count : \(senderMessageArray.count)")
print("Total Messages Count : \(conversationChatMessages.count)")
if section == 0
{
return senderMessageArray.count
}
else{
return receiveMessageArray.count
}
}
根据限制追加数组
conversationChatMessages = Mapper<Chats>().mapArray(JSONObject: chatObject)!
for receiveSms in conversationChatMessages
{
if receiveSms.sender_id != AppData.user_id()!
{
if (conversationData?.daysleft == 0)
{
if (todayLimit < 4)
{
self.receiveMessageArray.append(receiveSms)
todayLimit += 1
}
}
}
else
{
self.senderMessageArray.append(receiveSms)
}
问题是我先获取所有发件人消息,然后再按顺序获取收件人消息
答案 0 :(得分:1)
您需要听rmaddy所说的话。您不应该在cellForRowAt
中做出这些决定。这不是表视图的工作方式。
如果要更改是否存在某个单元格,请更改您的模型,以便numberOfSections
和numberOfRowsInSection
返回不同的答案。到cellForRowAt
被调用时,为时已晚:在该行中将有一个 单元,您的工作就是提供它。
同样,如果您想更改要显示在第1行中的单元格是A单元格还是B单元格,请更改您的模型,以便在{{1} },它将提供正确的单元格类型和数据。
答案 1 :(得分:1)
先前的回答是正确的,但这也许会有助于表达观点。
定义模型
self.vy
填充模型
def update(self):
self.vx = 0
self.vy += GRAVITY
def collide(self, xDif, yDif, platform_list):
for i in platform_list:
if pygame.sprite.collide_rect(self, i):
# Code omitted.
if yDif > 0:
self.rect.bottom = i.rect.top
self.state = self.player_states[0]
self.vy = 0 # Set vy to 0 if the sprite touches the ground.
并在表格视图数据源中实现
struct ConversationViewModel {
private var chatMessages = [ChatMessage]()
func rowsInChat() -> Int{
return chatMessages.count
}
func messageFor(_ indexPath: IndexPath) -> ChatMessage{
return chatMessages[indexPath.row]
}
func setChat(_ chatMessages: [ChatMessages] {
var finalMessages = chatMessages.filter{ $0.senderId == viewer }
var notMyMessages = chatMessages.filter{ $0.senderId != viewer}.sorted(by: {$0.date < $1.date } )
var limit = notMyMessagescount > accountLimit ? accountLimit : notMyMessagescount
finalMessages.append( notMyMessages[0..<limit] ).sorted(by: {$0.date < $1.date } )
chatMessages = finalMessages
//should probably notify VC that data changed
}
}
struct ChatMessage {
let senderId : String
let message : String
let date : Date
}